mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
hack chat example
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import trio_asyncio
|
||||||
|
import trio
|
||||||
import sys
|
import sys
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
@ -74,6 +76,10 @@ async def run(port: int, destination: str, localhost: bool) -> None:
|
|||||||
asyncio.ensure_future(write_data(stream))
|
asyncio.ensure_future(write_data(stream))
|
||||||
print("Connected to peer %s" % info.addrs[0])
|
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:
|
def main() -> None:
|
||||||
description = """
|
description = """
|
||||||
@ -112,15 +118,7 @@ def main() -> None:
|
|||||||
if not args.port:
|
if not args.port:
|
||||||
raise RuntimeError("was not able to determine a local port")
|
raise RuntimeError("was not able to determine a local port")
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
trio.run(async_main_wrapper, *(args.port, args.destination, args.localhost))
|
||||||
try:
|
|
||||||
asyncio.ensure_future(run(args.port, args.destination, args.localhost))
|
|
||||||
loop.run_forever()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
pass
|
|
||||||
finally:
|
|
||||||
loop.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
@ -24,18 +24,6 @@ from libp2p.transport.upgrader import TransportUpgrader
|
|||||||
from libp2p.typing import TProtocol
|
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:
|
def generate_new_rsa_identity() -> KeyPair:
|
||||||
return create_new_key_pair()
|
return create_new_key_pair()
|
||||||
|
|
||||||
@ -155,7 +143,4 @@ async def new_node(
|
|||||||
else:
|
else:
|
||||||
host = BasicHost(key_pair.public_key, swarm_opt)
|
host = BasicHost(key_pair.public_key, swarm_opt)
|
||||||
|
|
||||||
# Kick off cleanup job
|
|
||||||
asyncio.ensure_future(cleanup_done_tasks())
|
|
||||||
|
|
||||||
return host
|
return host
|
||||||
|
|||||||
Reference in New Issue
Block a user