Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
Testing RPC using ZMQ over TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
phlip9 committed Mar 22, 2016
1 parent 8ebc3ed commit 172ca88
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
26 changes: 26 additions & 0 deletions coinparty/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import asyncio
from aiozmq import rpc

# debugging:
# sudo tcpdump -i lo -f -X -S -s 0 'tcp port 2205'

async def run(server_addr):
print('connecting to rpc server...')
client = await rpc.connect_rpc(connect=server_addr, timeout=5)

for i in range(10):
res = await client.call.add(123, 456)
print('add_server.add(4, 2) = %d' % res)
await asyncio.sleep(3)

print('closing client')
client.close()
await client.wait_closed()

# server_addr = 'ipc://add_server.ipc'
server_addr = 'tcp://127.0.0.1:2205'

loop = asyncio.get_event_loop()
loop.run_until_complete(run(server_addr))
print('closing event loop')
loop.close()
50 changes: 50 additions & 0 deletions coinparty/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import asyncio
from aiozmq import rpc
import signal
import os

class Handler(rpc.AttrHandler):

@rpc.method
async def add(self, a: int, b: int) -> int:
print('add %d, %d' % (a, b))
await asyncio.sleep(3)
print('finished sleeping')
return a + b

def hidden(self):
return 'wtf'

def close(loop, server, signame):
print('close(): got signal %s: closing' % signame)
print('close(): closing server')
server.close()
print('close(): closing event loop')
loop.stop()

async def run(loop, server_addr):
print('rpc starting server...')
server = await rpc.serve_rpc(
Handler(),
bind=server_addr,
log_exceptions=True
)

for signame in ('SIGINT', 'SIGTERM'):
loop.add_signal_handler(
getattr(signal, signame),
lambda: close(loop, server, signame)
)

# server_addr = 'ipc://add_server.ipc'
server_addr = 'tcp://127.0.0.1:2205'
print('server running at "%s", pid: %s' % (server_addr, os.getpid()))

loop = asyncio.get_event_loop()
loop.create_task(run(loop, server_addr))

try:
loop.run_forever()
finally:
print('closing event loop')
loop.close()

0 comments on commit 172ca88

Please sign in to comment.