Skip to content
Snippets Groups Projects
Commit 5a7c8753 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

python/tools: use asyncio in pingpong test

parent 19fdec96
Branches
Tags
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
import opendht as dht import opendht as dht
import time import time
import asyncio
ping_node = dht.DhtRunner() ping_node = dht.DhtRunner()
ping_node.run(port = 5632) ping_node.run(port = 5632)
...@@ -35,19 +36,28 @@ loc_pong = dht.InfoHash.get(str(loc_ping)) ...@@ -35,19 +36,28 @@ loc_pong = dht.InfoHash.get(str(loc_ping))
i = 0 i = 0
MAX = 8192 MAX = 8192
def pong(node, h): loop = asyncio.get_event_loop()
def done(h, ok):
print(h, "over", ok)
def ping(node, h):
global i global i
print("got ping", h, i)
time.sleep(0.0075) time.sleep(0.0075)
i += 1 i += 1
if i < MAX: if i < MAX:
node.put(h, dht.Value(b"hey"), lambda ok, nodes: print("over", ok)) node.put(h, dht.Value(b"hey"), lambda ok, nodes: done(node.getNodeId().decode(), ok))
else:
loop.stop()
def pong(node, h):
print(node.getNodeId().decode(), "got ping", h, i)
loop.call_soon_threadsafe(ping, node, h);
return True return True
ping_node.listen(loc_ping, lambda v: pong(pong_node, loc_pong)) ping_node.listen(loc_ping, lambda v: pong(pong_node, loc_pong))
pong_node.listen(loc_pong, lambda v: pong(ping_node, loc_ping)) pong_node.listen(loc_pong, lambda v: pong(ping_node, loc_ping))
pong_node.put(loc_pong, dht.Value(b"hey"), lambda ok, nodes: print("i over", ok)) ping(pong_node, loc_ping)
while (i < MAX): loop.run_forever()
time.sleep(1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment