Search code examples
python-3.xzeromqblockingpython-asyncio

python3 asyncio ZeroMQ .connect() blocks


I try to implement a REQ/REP pattern, with python3 asyncio and ZeroMQ

My client async function:

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())

As I understand, the call to a socket.connect( "tcp://11.111.11.245:5555" ) method is a blocking method.

How to make a non-blocking connection call, in my case?


Solution

  • As far as I understand the ZeroMQ API, the call to .connect() method is not synchronous with building the real connection ( if not introduced by the wrapper, the underlying API is non-blocking - ref. below ).

    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.

    Ref.: ZeroMQ API - zmq_connect(3)