Skip to content

Commit 6f0fe54

Browse files
committed
Allow AsyncioDispatcher to take existing event loop
1 parent 8b71667 commit 6f0fe54

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

‎softioc/asyncio_dispatcher.py‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,18 @@
33
import threading
44

55

6-
class AsyncioDispatcher(threading.Thread):
7-
"""A dispatcher for `asyncio` based IOCs. Means that `on_update` callback
8-
functions can be async. Will run an Event Loop in a thread when
9-
created.
10-
"""
11-
def __init__(self):
12-
"""Create an AsyncioDispatcher suitable to be used by
13-
`softioc.iocInit`."""
14-
# Docstring specified to suppress threading.Thread's docstring, which
15-
# would otherwise be inherited by this method and be misleading.
16-
super().__init__()
6+
class AsyncioDispatcher:
7+
def __init__(self, loop=None):
8+
"""A dispatcher for `asyncio` based IOCs, suitable to be passed to
9+
`softioc.iocInit`. Means that `on_update` callback functions can be
10+
async. If loop is None, will run an Event Loop in a thread when created.
11+
"""
1712
#: `asyncio` event loop that the callbacks will run under.
18-
self.loop = asyncio.new_event_loop()
19-
self.start()
20-
21-
def run(self):
22-
self.loop.run_forever()
13+
self.loop = loop
14+
if loop is None:
15+
# Make one and run it in a background thread
16+
self.loop = asyncio.new_event_loop()
17+
threading.Thread(target=self.loop.run_forever).start()
2318

2419
def __call__(self, func, *args):
2520
async def async_wrapper():

0 commit comments

Comments
 (0)