Skip to content

Commit ae620b1

Browse files
committed
async event listener
1 parent 70ca284 commit ae620b1

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/sqlalchemy/event/api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ def on_config():
9696
9797
"""
9898

99+
if util.concurrency.iscoroutinefunction(fn):
100+
await_ = (
101+
util.concurrency.await_fallback
102+
if kw.pop("async_fallback", False)
103+
else util.concurrency.await_only
104+
)
105+
listener = fn
106+
107+
def fn(*_args, **kwargs):
108+
return await_(listener(*_args, **kwargs))
109+
99110
_event_key(target, identifier, fn).listen(*args, **kw)
100111

101112

lib/sqlalchemy/util/concurrency.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pass
1010
else:
1111
have_greenlet = True
12+
from inspect import iscoroutinefunction
1213
from ._concurrency_py3k import await_only
1314
from ._concurrency_py3k import await_fallback
1415
from ._concurrency_py3k import greenlet_spawn
@@ -48,3 +49,6 @@ def _util_async_run(fn, *arg, **kw): # noqa F81
4849

4950
def _util_async_run_coroutine_function(fn, *arg, **kw): # noqa F81
5051
_not_implemented()
52+
53+
def iscoroutinefunction(fn):
54+
return False

0 commit comments

Comments
 (0)