Skip to content

Commit 68a9163

Browse files
chenyumiccguardia
authored andcommitted
Implemented tasklets.synctasklet (#58)
* Implemented tasklets.synctasklet
1 parent 61f4144 commit 68a9163

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

packages/google-cloud-ndb/src/google/cloud/ndb/tasklets.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,23 @@ def set_context(*args, **kwargs):
502502
raise NotImplementedError
503503

504504

505-
def synctasklet(*args, **kwargs):
506-
raise NotImplementedError
505+
def synctasklet(wrapped):
506+
"""A decorator to run a tasklet as a function when called.
507+
508+
Use this to wrap a request handler function that will be called by some
509+
web application framework (e.g. a Django view function or a
510+
webapp.RequestHandler.get method).
511+
512+
Args:
513+
wrapped (callable): The wrapped function.
514+
"""
515+
taskletfunc = tasklet(wrapped)
516+
517+
@functools.wraps(wrapped)
518+
def synctasklet_wrapper(*args, **kwargs):
519+
return taskletfunc(*args, **kwargs).result()
520+
521+
return synctasklet_wrapper
507522

508523

509524
def toplevel(*args, **kwargs):

packages/google-cloud-ndb/tests/unit/test_tasklets.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,17 @@ def test_set_context():
593593
tasklets.set_context()
594594

595595

596+
@pytest.mark.usefixtures("in_context")
596597
def test_synctasklet():
597-
with pytest.raises(NotImplementedError):
598-
tasklets.synctasklet()
598+
@tasklets.synctasklet
599+
def generator_function(value):
600+
future = tasklets.Future(value)
601+
future.set_result(value)
602+
x = yield future
603+
return x + 3
604+
605+
result = generator_function(8)
606+
assert result == 11
599607

600608

601609
def test_toplevel():

0 commit comments

Comments
 (0)