diff --git a/effect/__init__.py b/effect/__init__.py index b03abfd..4db92c3 100644 --- a/effect/__init__.py +++ b/effect/__init__.py @@ -8,7 +8,7 @@ """ from ._base import Effect, perform, NoPerformerFoundError, catch, raise_ -from ._sync import NotSynchronousError, sync_perform, sync_performer +from ._sync import NotSynchronousError, async_perform, sync_performer from ._intents import ( Delay, perform_delay_with_sleep, @@ -27,7 +27,7 @@ __all__ = [ # Order here affects the order that these things show up in the API docs. "Effect", - "sync_perform", + "async_perform", "sync_performer", "base_dispatcher", "TypeDispatcher", diff --git a/effect/_base.py b/effect/_base.py index efaef5c..36fe168 100644 --- a/effect/_base.py +++ b/effect/_base.py @@ -47,11 +47,11 @@ def __init__(self, cont): """ self._cont = cont - def succeed(self, result): + async def succeed(self, result): """ Indicate that the effect has succeeded, and the result is available. """ - self._cont((False, result)) + await self._cont((False, result)) def fail(self, result): """ @@ -77,7 +77,7 @@ class NoPerformerFoundError(Exception): """Raised when a performer for an intent couldn't be found.""" -def perform(dispatcher, effect): +async def perform(dispatcher, effect): """ Perform an effect and invoke callbacks bound to it. You probably don't want to use this. Instead, use :func:`sync_perform` (or, if you're using @@ -120,11 +120,11 @@ def perform(dispatcher, effect): an exception. Decorators like :func:`sync_performer` simply abstract this away. """ - def _run_callbacks(bouncer, chain, result): + async def _run_callbacks(bouncer, chain, result): is_error, value = result if type(value) is Effect: - bouncer.bounce( + await bouncer.bounce( _perform, Effect(value.intent, callbacks=value.callbacks + chain) ) return @@ -136,15 +136,15 @@ def _run_callbacks(bouncer, chain, result): if cb is not None: result = guard(cb, value) chain = chain[1:] - bouncer.bounce(_run_callbacks, chain, result) + await bouncer.bounce(_run_callbacks, chain, result) - def _perform(bouncer, effect): + async def _perform(bouncer, effect): try: performer = dispatcher(effect.intent) if performer is None: raise NoPerformerFoundError(effect.intent) else: - performer( + await performer( dispatcher, effect.intent, _Box(partial(bouncer.bounce, _run_callbacks, effect.callbacks)), @@ -152,7 +152,7 @@ def _perform(bouncer, effect): except Exception as e: _run_callbacks(bouncer, effect.callbacks, (True, e)) - trampoline(_perform, effect) + await trampoline(_perform, effect) def catch(exc_type, callable): diff --git a/effect/_continuation.py b/effect/_continuation.py index 042303c..b3cc59c 100644 --- a/effect/_continuation.py +++ b/effect/_continuation.py @@ -5,7 +5,7 @@ class Bouncer(object): work = None _asynchronous = False - def bounce(self, func, *args, **kwargs): + async def bounce(self, func, *args, **kwargs): """ Bounce a function off the trampoline -- in other words, signal to the trampoline that the given function should be run. It will be passed a @@ -23,11 +23,11 @@ def bounce(self, func, *args, **kwargs): ) self.work = (func, args, kwargs) if self._asynchronous: - trampoline(func, *args, **kwargs) + await trampoline(func, *args, **kwargs) return -def trampoline(f, *args, **kwargs): +async def trampoline(f, *args, **kwargs): """ An asynchronous trampoline. @@ -54,7 +54,7 @@ def trampoline(f, *args, **kwargs): """ while True: bouncer = Bouncer() - f(bouncer, *args, **kwargs) + await f(bouncer, *args, **kwargs) if bouncer.work is not None: f, args, kwargs = bouncer.work else: diff --git a/effect/_sync.py b/effect/_sync.py index f7a8cdd..59afa32 100644 --- a/effect/_sync.py +++ b/effect/_sync.py @@ -12,7 +12,7 @@ class NotSynchronousError(Exception): """Performing an effect did not immediately return a value.""" -def sync_perform(dispatcher, effect): +async def async_perform(dispatcher, effect): """ Perform an effect, and return its ultimate result. If the final result is an error, the exception will be raised. @@ -24,7 +24,7 @@ def sync_perform(dispatcher, effect): successes = [] errors = [] effect = effect.on(success=successes.append, error=errors.append) - perform(dispatcher, effect) + await perform(dispatcher, effect) if successes: return successes[0] elif errors: