This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |