-
Notifications
You must be signed in to change notification settings - Fork 1.6k
WIP: NDB: Async context #6429
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
WIP: NDB: Async context #6429
Conversation
accomplished in a middleware layer. | ||
""" | ||
loop = _eventloop.contexts.push() | ||
yield |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ndb/src/google/cloud/ndb/async_.py
Outdated
|
||
This function provides a context manager which establishes the event loop | ||
that will be used for any asynchronous NDB calls that occur in the context. | ||
For example:: |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ndb/src/google/cloud/ndb/async_.py
Outdated
loop = _eventloop.contexts.push() | ||
yield | ||
_eventloop.contexts.pop() | ||
loop.run() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ndb/tests/unit/test_async_.py
Outdated
@@ -0,0 +1,24 @@ | |||
import unittest |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ndb/tests/unit/test_async_.py
Outdated
from google.cloud.ndb import async_ | ||
|
||
|
||
@unittest.mock.patch("google.cloud.ndb.async_._eventloop.EventLoop") |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ndb/tests/unit/test_async_.py
Outdated
|
||
@unittest.mock.patch("google.cloud.ndb.async_._eventloop.EventLoop") | ||
def test_async_context(EventLoop): | ||
one = unittest.mock.Mock() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ndb/tests/unit/test_async_.py
Outdated
def test_async_context(EventLoop): | ||
one = unittest.mock.Mock() | ||
two = unittest.mock.Mock() | ||
EventLoop.return_value = one |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ndb/tests/unit/test_async_.py
Outdated
one.run.assert_not_called() | ||
two.run.assert_called_once_with() | ||
assert _eventloop.contexts.current() is None | ||
one.run.assert_called_once_with() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
f0673b5
to
acbe003
Compare
@@ -126,6 +127,7 @@ | |||
from google.cloud.ndb.context import ContextOptions | |||
from google.cloud.ndb.context import EVENTUAL_CONSISTENCY | |||
from google.cloud.ndb.context import TransactionOptions | |||
from google.cloud.ndb._eventloop import async_context |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
loop = EventLoop() | ||
contexts.push(loop) | ||
yield | ||
contexts.pop() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
In the context of a web application, it is recommended that a single | ||
asynchronous context be used per HTTP request. This can typically be | ||
accomplished in a middleware layer. | ||
""" |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
||
Within the context, any calls to a ``*_async`` function or to an | ||
``ndb.tasklet``, will be added to the event loop established by the | ||
context. Upon exiting the context, execution will block until all |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Establish an async context for managing the current event loop for asynchronous communication with the datastore backend.
Work in progress. Sharing early so I can get feedback.