python3 asyncio ZeroMQ .connect() 块
python3 asyncio ZeroMQ .connect() blocks
我尝试使用 python3 asyncio 和 ZeroMQ
实现 REQ/REP
模式
我的客户端异步函数:
import zmq
import os
from time import time
import asyncio
import zmq.asyncio
print ('Client %i'%os.getpid())
context = zmq.asyncio.Context(1)
loop = zmq.asyncio.ZMQEventLoop()
asyncio.set_event_loop(loop)
async def client():
socket = context.socket(zmq.REQ)
socket.connect('tcp://11.111.11.245:5555')
while True:
data = zmq.Message(str(os.getpid()).encode('utf8'))
start = time()
print('send')
await socket.send(data)
print('wait...')
data = await socket.recv()
print('recv')
print(time() - start, data)
loop.run_until_complete(client())
据我了解,调用 socket.connect( "tcp://11.111.11.245:5555" )
方法是一种阻塞方法。
在我的例子中,如何进行非阻塞连接调用?
据我了解 ZeroMQ API,对 .connect()
方法的调用与构建真实连接不同步(如果不是由包装器,底层 API 是非阻塞的 - 参考下面 ).
The connection will not be performed immediately but as needed by ØMQ. Thus a successful invocation of zmq_connect()
does not indicate that a physical connection was or can actually be established.
我尝试使用 python3 asyncio 和 ZeroMQ
实现REQ/REP
模式
我的客户端异步函数:
import zmq
import os
from time import time
import asyncio
import zmq.asyncio
print ('Client %i'%os.getpid())
context = zmq.asyncio.Context(1)
loop = zmq.asyncio.ZMQEventLoop()
asyncio.set_event_loop(loop)
async def client():
socket = context.socket(zmq.REQ)
socket.connect('tcp://11.111.11.245:5555')
while True:
data = zmq.Message(str(os.getpid()).encode('utf8'))
start = time()
print('send')
await socket.send(data)
print('wait...')
data = await socket.recv()
print('recv')
print(time() - start, data)
loop.run_until_complete(client())
据我了解,调用 socket.connect( "tcp://11.111.11.245:5555" )
方法是一种阻塞方法。
在我的例子中,如何进行非阻塞连接调用?
据我了解 ZeroMQ API,对 .connect()
方法的调用与构建真实连接不同步(如果不是由包装器,底层 API 是非阻塞的 - 参考下面 ).
The connection will not be performed immediately but as needed by ØMQ. Thus a successful invocation of
zmq_connect()
does not indicate that a physical connection was or can actually be established.