File tree Expand file tree Collapse file tree 2 files changed +27
-4
lines changed
packages/google-cloud-ndb Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Original file line number Diff line number Diff 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
509524def toplevel (* args , ** kwargs ):
Original file line number Diff line number Diff line change @@ -593,9 +593,17 @@ def test_set_context():
593593 tasklets .set_context ()
594594
595595
596+ @pytest .mark .usefixtures ("in_context" )
596597def 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
601609def test_toplevel ():
You can’t perform that action at this time.
0 commit comments