-
Notifications
You must be signed in to change notification settings - Fork 56
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
81 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
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,74 @@ | ||
import asyncio | ||
import aiozmq | ||
import zmq | ||
|
||
|
||
class ZmqDealerProtocol(aiozmq.ZmqProtocol): | ||
|
||
transport = None | ||
|
||
def __init__(self, queue, on_close): | ||
self.queue = queue | ||
self.on_close = on_close | ||
|
||
def connection_made(self, transport): | ||
self.transport = transport | ||
|
||
def msg_received(self, msg): | ||
self.queue.put_nowait(msg) | ||
|
||
def connection_lost(self, exc): | ||
self.on_close.set_result(exc) | ||
|
||
|
||
class ZmqRouterProtocol(aiozmq.ZmqProtocol): | ||
|
||
transport = None | ||
|
||
def __init__(self, on_close): | ||
self.on_close = on_close | ||
|
||
def connection_made(self, transport): | ||
self.transport = transport | ||
|
||
def msg_received(self, msg): | ||
self.transport.write(msg) | ||
|
||
def connection_lost(self, exc): | ||
self.on_close.set_result(exc) | ||
|
||
|
||
@asyncio.coroutine | ||
def go(): | ||
router_closed = asyncio.Future() | ||
dealer_closed = asyncio.Future() | ||
router, _ = yield from aiozmq.create_zmq_connection( | ||
lambda: ZmqRouterProtocol(router_closed), | ||
zmq.ROUTER, | ||
bind='tcp://127.0.0.1:*') | ||
|
||
addr = next(iter(router.bindings())) | ||
queue = asyncio.Queue() | ||
dealer, _ = yield from aiozmq.create_zmq_connection( | ||
lambda: ZmqDealerProtocol(queue, dealer_closed), | ||
zmq.DEALER, | ||
connect=addr) | ||
|
||
for i in range(10): | ||
msg = (b'data', b'ask', str(i).encode('utf-8')) | ||
dealer.write(msg) | ||
answer = yield from queue.get() | ||
print(answer) | ||
dealer.close() | ||
yield from dealer_closed | ||
router.close() | ||
yield from router_closed | ||
|
||
|
||
def main(): | ||
asyncio.get_event_loop().run_until_complete(go()) | ||
print("DONE") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |