-
Notifications
You must be signed in to change notification settings - Fork 394
Implement simple asyncio wrapper API with basic tests #646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Traktormaster
wants to merge
8
commits into
python-zk:master
Choose a base branch
from
Traktormaster:aiome
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa6554c
Implement simple asyncio wrapper API with basic tests
2d80d64
Line lengths
45f4924
Line lengths
040d649
Line lengths
e2419bc
Fix start_aio() and include it in test
2b34499
Implement an asyncio compatible retry utility based on KazooRetry
2297a9c
Improved implementation to avoid creating state-objects
dd9a441
Differentiate the purpose/scope of new async-result objects to optimi…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """ | ||
| Simple asyncio integration of the threaded async executor engine. | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import asyncio | ||
|
|
||
| from kazoo.aio.handler import AioSequentialThreadingHandler | ||
| from kazoo.client import KazooClient, TransactionRequest | ||
|
|
||
|
|
||
| class AioKazooClient(KazooClient): | ||
| """ | ||
| The asyncio compatibility mostly mimics the behaviour of the base async one. All calls are wrapped in | ||
| asyncio.shield() to prevent cancellation that is not supported in the base async implementation. | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
| The sync and base-async API are still completely functional. Mixing the use of any of the 3 should be okay. | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| """ | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| if not kwargs.get("handler"): | ||
| kwargs["handler"] = AioSequentialThreadingHandler() | ||
| KazooClient.__init__(self, *args, **kwargs) | ||
|
|
||
| # asyncio compatible api wrappers | ||
| async def start_aio(self): | ||
| return await asyncio.shield(self.start_async().future) | ||
|
|
||
| async def add_auth_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.add_auth_async(*args, **kwargs).future) | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
| async def sync_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.sync_async(*args, **kwargs).future) | ||
|
|
||
| async def create_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.create_async(*args, **kwargs).future) | ||
|
|
||
| async def ensure_path_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.ensure_path_async(*args, **kwargs).future) | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
| async def exists_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.exists_async(*args, **kwargs).future) | ||
|
|
||
| async def get_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.get_async(*args, **kwargs).future) | ||
|
|
||
| async def get_children_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.get_children_async(*args, **kwargs).future) | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
| async def get_acls_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.get_acls_async(*args, **kwargs).future) | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
| async def set_acls_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.set_acls_async(*args, **kwargs).future) | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
| async def set_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.set_async(*args, **kwargs).future) | ||
|
|
||
| def transaction_aio(self): | ||
| return AioTransactionRequest(self) | ||
|
|
||
| async def delete_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.delete_async(*args, **kwargs).future) | ||
|
|
||
| async def reconfig_aio(self, *args, **kwargs): | ||
| return await asyncio.shield(self.reconfig_async(*args, **kwargs).future) | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| class AioTransactionRequest(TransactionRequest): | ||
| async def commit_aio(self): | ||
| return await asyncio.shield(self.commit_async().future) | ||
|
|
||
| async def __aenter__(self): | ||
| return self | ||
|
|
||
| async def __aexit__(self, exc_type, exc_value, exc_tb): | ||
| if not exc_type: | ||
| await self.commit_aio() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import asyncio | ||
| import threading | ||
|
|
||
| from kazoo.handlers.threading import AsyncResult, SequentialThreadingHandler | ||
|
|
||
|
|
||
| class AioAsyncResult(AsyncResult): | ||
| def __init__(self, handler): | ||
| self.future = handler.loop.create_future() | ||
| AsyncResult.__init__(self, handler) | ||
|
|
||
| def set(self, value=None): | ||
| """ | ||
| The completion of the future has the same guarantees as the notification emitting of the condition. | ||
| Provided that no callbacks raise it will complete. | ||
| """ | ||
| AsyncResult.set(self, value) | ||
| self._handler.loop.call_soon_threadsafe(self.future.set_result, value) | ||
|
|
||
| def set_exception(self, exception): | ||
| """ | ||
| The completion of the future has the same guarantees as the notification emitting of the condition. | ||
| Provided that no callbacks raise it will complete. | ||
| """ | ||
| AsyncResult.set_exception(self, exception) | ||
| self._handler.loop.call_soon_threadsafe(self.future.set_exception, exception) | ||
|
|
||
|
|
||
| class AioSequentialThreadingHandler(SequentialThreadingHandler): | ||
| def __init__(self): | ||
| """ | ||
| Creating the handler must be done on the asyncio-loop's thread. | ||
| """ | ||
| self.loop = asyncio.get_running_loop() | ||
| self._aio_thread = threading.current_thread() | ||
| SequentialThreadingHandler.__init__(self) | ||
|
|
||
| def async_result(self): | ||
| """ | ||
| Almost all async-result objects are created by a method that is invoked from the user's thead. The | ||
| one exception I'm aware of is in the PatientChildrenWatch utility, that creates an async-result in | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| its worker thread. Just because of that it is imperative to only create asyncio compatible results | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| when the invoking code is from the loop's thread. There is no PEP/API guarantee that implementing | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| the create_future() has to be thread-safe. The default is mostly thread-safe. The only thing that | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| may get synchronization issue is a debug-feature for asyncio development. Quickly looking at the | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| alternate implementation of uvloop, they use the default Future implementation, so no change there. | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| For now, just to be safe, we check the current thread and create an async-result object based on the | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
| invoking thread's identity. | ||
| """ | ||
| if threading.current_thread() is self._aio_thread: | ||
| return AioAsyncResult(self) | ||
| return AsyncResult(self) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| from kazoo.testing.harness import KazooTestCase, KazooTestHarness | ||
| from kazoo.testing.harness import KazooAioTestCase, KazooTestCase, KazooTestHarness | ||
|
Traktormaster marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| __all__ = ('KazooTestHarness', 'KazooTestCase', ) | ||
| __all__ = ('KazooTestHarness', 'KazooTestCase', 'KazooAioTestCase', ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from kazoo.exceptions import NotEmptyError, NoNodeError | ||
| from kazoo.protocol.states import ZnodeStat | ||
| from kazoo.testing import KazooAioTestCase | ||
|
|
||
|
|
||
| class KazooAioTests(KazooAioTestCase): | ||
| def test_basic_aio_functionality(self): | ||
| self.loop.run_until_complete(self._test_basic_aio_functionality()) | ||
|
|
||
| async def _test_basic_aio_functionality(self): | ||
| assert await self.client.create_aio("/tmp") == "/tmp" | ||
| assert await self.client.get_children_aio("/") == ["tmp"] | ||
| assert await self.client.ensure_path_aio("/tmp/x/y") == "/tmp/x/y" | ||
| assert await self.client.exists_aio("/tmp/x/y") | ||
| assert isinstance(await self.client.set_aio("/tmp/x/y", b"very aio"), ZnodeStat) | ||
| data, stat = await self.client.get_aio("/tmp/x/y") | ||
| assert data == b"very aio" | ||
| assert isinstance(stat, ZnodeStat) | ||
| try: | ||
| await self.client.delete_aio("/tmp/x") | ||
| except NotEmptyError: | ||
| pass | ||
| await self.client.delete_aio("/tmp/x/y") | ||
| try: | ||
| await self.client.get_aio("/tmp/x/y") | ||
| except NoNodeError: | ||
| pass | ||
| async with self.client.transaction_aio() as tx: | ||
| tx.create("/tmp/z", b"ZZZ") | ||
| tx.set_data("/tmp/x", b"XXX") | ||
| assert (await self.client.get_aio("/tmp/x"))[0] == b"XXX" | ||
| assert (await self.client.get_aio("/tmp/z"))[0] == b"ZZZ" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.