Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 11 commits into from
Nov 8, 2018
Merged

Conversation

chrisrossi
Copy link

Work in progress. Sharing early so I can get feedback.

@chrisrossi chrisrossi requested a review from crwilcox as a code owner November 6, 2018 21:26
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Nov 6, 2018
@chrisrossi chrisrossi added do not merge Indicates a pull request not ready for merge, due to either quality or timing. api: ndb labels Nov 6, 2018
accomplished in a middleware layer.
"""
loop = _eventloop.contexts.push()
yield

This comment was marked as spam.


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.

loop = _eventloop.contexts.push()
yield
_eventloop.contexts.pop()
loop.run()

This comment was marked as spam.

This comment was marked as spam.

@@ -0,0 +1,24 @@
import unittest

This comment was marked as spam.

This comment was marked as spam.

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.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


@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.

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.

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.

@@ -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.

This comment was marked as spam.

loop = EventLoop()
contexts.push(loop)
yield
contexts.pop()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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.

@chrisrossi chrisrossi removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Nov 8, 2018
@chrisrossi
Copy link
Author

@tseaver @dhermes I think I've addressed everyone's comments. PTAL.

Copy link
Contributor

@dhermes dhermes left a 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.

@chrisrossi chrisrossi merged commit 8517524 into googleapis:master Nov 8, 2018
@chrisrossi chrisrossi deleted the ndb-async-context branch November 8, 2018 19:26
chrisrossi pushed a commit to chrisrossi/google-cloud-python that referenced this pull request Feb 22, 2019
Establish an async context for managing the current event loop for asynchronous communication with the datastore backend.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: ndb cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants