Skip to content

Commit

Permalink
Cleanup async code
Browse files Browse the repository at this point in the history
- fully switches to the recommended high-level API by not interfering with the loop in any way
- removes the need to manually cleanup tasks by having to wait for a signal
- fixes the problem with the update feeds task where exceptions where only shown on shutdown

This is the first part of the fix for jplitza#10
  • Loading branch information
herndlm committed Aug 22, 2021
1 parent a6001ae commit 8c7a9b3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.
17 changes: 2 additions & 15 deletions nina_xmpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,10 @@ async def run(self):
None,
self.message_received,
)

update_feeds_task = asyncio.create_task(self.update_feeds_task())
await self.make_sigint_event().wait()
update_feeds_task.cancel()

try:
await update_feeds_task
except asyncio.CancelledError:
pass

def make_sigint_event(self):
event = asyncio.Event()
loop = asyncio.get_event_loop()
loop.add_signal_handler(
signal.SIGINT,
event.set,
)
return event
await asyncio.wait({update_feeds_task})

def message_received(self, msg):
if not msg.body:
Expand Down
6 changes: 1 addition & 5 deletions nina_xmpp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ def main():

main = NinaXMPP(config)

loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main.run())
finally:
loop.close()
asyncio.run(main.run())


if __name__ == '__main__':
Expand Down

0 comments on commit 8c7a9b3

Please sign in to comment.