mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
hack chat example
This commit is contained in:
@ -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()
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user