-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add activate_async decorator * make activate_async python 3.4 compatible * unify the import statement
- Loading branch information
Showing
2 changed files
with
47 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import functools | ||
from asyncio import iscoroutinefunction, coroutine | ||
|
||
|
||
def activate_async(fn, _engine): | ||
""" | ||
Async version of activate decorator | ||
Arguments: | ||
fn (function): function that be wrapped by decorator. | ||
_engine (Engine): pook engine instance | ||
Returns: | ||
function: decorator wrapper function. | ||
""" | ||
@functools.wraps(fn) | ||
@coroutine | ||
def wrapper(*args, **kw): | ||
_engine.activate() | ||
try: | ||
if iscoroutinefunction(fn): | ||
yield from fn(*args, **kw) # noqa | ||
else: | ||
fn(*args, **kw) | ||
except Exception as err: | ||
raise err | ||
finally: | ||
_engine.disable() | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters