added shutdown and endlisteners

master
Ugo Finnendahl 5 years ago
parent f93180bbc7
commit 8ab7c95c98
  1. 15
      ewents/__init__.py

@ -12,7 +12,7 @@ async def emit(signal, payload=None):
await q.put({})
async def shutdown():
async def a_shutdown():
for key in global_signals:
for q in global_signals[key]:
await q.put(None)
@ -21,7 +21,7 @@ async def shutdown():
def shutdown(loop=None):
if loop is None:
loop = asyncio.get_event_loop()
loop.run_until_complete(shutdown())
loop.run_until_complete(a_shutdown())
async def consumer(queue, callback, signal):
@ -35,6 +35,13 @@ async def consumer(queue, callback, signal):
else:
await callback()
class ListenerHandle:
def __init__(self, queue):
self.queue = queue
async def close(self):
await self.queue.put(None)
def listen_on(signal, fun=None, *, loop=None):
if loop is None:
@ -47,13 +54,13 @@ def listen_on(signal, fun=None, *, loop=None):
q = asyncio.Queue()
global_signals.setdefault(signal, []).append(q)
handle = loop.call_soon(
loop.call_soon(
lambda: asyncio.ensure_future(consumer(q, func, signal))
)
if deco:
return fun
else:
return handle
return ListenerHandle(q)
if fun is not None:
return decorator(fun, False)

Loading…
Cancel
Save