Skip to content

Commit

Permalink
Keep a reference to tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Mar 16, 2023
1 parent 69da86f commit c4e0ff3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/userguide/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Create a ``tests`` directory at the root of the project directory and create a m
client = ClientComponent("Hello!")
await client.start(ctx)

event_loop.create_task(run())
task = event_loop.create_task(run())
with pytest.raises(SystemExit) as exc:
event_loop.run_forever()

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial1/tests/test_client_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def run() -> None:
client = ClientComponent("Hello!")
await client.start(ctx)

event_loop.create_task(run())
task = event_loop.create_task(run())
with pytest.raises(SystemExit) as exc:
event_loop.run_forever()

Expand Down
5 changes: 4 additions & 1 deletion src/asphalt/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def __init__(
event_class, Event
), "event_class must be a subclass of Event"
self.bound_signals: MutableMapping[Any, Signal] = WeakKeyDictionary()
self._background_tasks = set()

def __get__(self, instance, owner) -> Signal:
if instance is None:
Expand Down Expand Up @@ -225,7 +226,9 @@ async def do_dispatch() -> None:
future = loop.create_future()
if self.listeners:
listeners = list(self.listeners)
loop.create_task(do_dispatch())
task = loop.create_task(do_dispatch())
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
else:
future.set_result(True)

Expand Down

0 comments on commit c4e0ff3

Please sign in to comment.