diff --git a/examples/chat/chat.py b/examples/chat/chat.py index 24c92699..73436a0d 100755 --- a/examples/chat/chat.py +++ b/examples/chat/chat.py @@ -1,5 +1,7 @@ import argparse import asyncio +import trio_asyncio +import trio import sys import urllib.request @@ -74,6 +76,10 @@ async def run(port: int, destination: str, localhost: bool) -> None: asyncio.ensure_future(write_data(stream)) print("Connected to peer %s" % info.addrs[0]) +async def async_main_wrapper(*args): + async with trio_asyncio.open_loop() as loop: + assert loop == asyncio.get_event_loop() + await run(*args) def main() -> None: description = """ @@ -112,15 +118,7 @@ def main() -> None: if not args.port: raise RuntimeError("was not able to determine a local port") - loop = asyncio.get_event_loop() - try: - asyncio.ensure_future(run(args.port, args.destination, args.localhost)) - loop.run_forever() - except KeyboardInterrupt: - pass - finally: - loop.close() - + trio.run(async_main_wrapper, *(args.port, args.destination, args.localhost)) if __name__ == "__main__": main() diff --git a/libp2p/__init__.py b/libp2p/__init__.py index a1dca535..3359f7de 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -24,18 +24,6 @@ from libp2p.transport.upgrader import TransportUpgrader from libp2p.typing import TProtocol -async def cleanup_done_tasks() -> None: - """clean up asyncio done tasks to free up resources.""" - while True: - for task in asyncio.all_tasks(): - if task.done(): - await task - - # Need not run often - # Some sleep necessary to context switch - await asyncio.sleep(3) - - def generate_new_rsa_identity() -> KeyPair: return create_new_key_pair() @@ -155,7 +143,4 @@ async def new_node( else: host = BasicHost(key_pair.public_key, swarm_opt) - # Kick off cleanup job - asyncio.ensure_future(cleanup_done_tasks()) - return host