zmq.error.ZMQError:非套接字上的套接字操作:如何在关闭之前查找活动套接字?

时间:2017-07-27 21:36:26

标签: python zeromq pyzmq

在我的应用程序中,我创建了2个套接字并尝试/除此之外:

try:
    socketA.connect("tcp://localhost:5557")
    socketB.bind("tcp://localhost:5558")
except zmq.ZMQError as e:

    if e.errno == zmq.EINVAL:
        logger.error("Endpoint supplied is invalid")
    else:
        logger.error("The ZeroMQ error with an error number {0}".format(e.errno))
        raise ZMQError(e)
    cleanUp()

如果由于某种原因,其中一个套接字不能.connect() / .bind(),我想关闭两个套接字并在cleanUp()函数中终止上下文,但我怎么知道哪个插座在关闭前是活着的吗?

在关闭活动套接字之前,ZeroMQ是否提供有关活动套接字的任何信息?

2 个答案:

答案 0 :(得分:1)

鉴于上述逻辑,让我们使用另一种方法:

案例A:两个套接字分别.connect() + .bind() 案例B:任何套接字确实都失败了。

 try:
       socketA.connect(   "tcp://localhost:5557" )
       socketA.setsockopt( zmq.LINGER, 0 )
       try:
            socketB.bind(      "tcp://localhost:5558" )
            socketB.setsockopt( zmq.LINGER, 0 )

       except zmq.ZMQError as e:

            if ( e.errno in ( zmq.EINVAL, 
                              zmq.EPROTONOSUPPORT,
                              zmq.ENOCOMPATPROTO,
                              zmq.EADDRINUSE,
                              zmq.EADDRNOTAVAIL,
                              )
                 ):
                 logger.error( "ZeroMQ TransportClass / Endpoint cannot be setup for [socketB]." )

            if ( e.errno in ( zmq.ENODEV,
                              zmq.ENOTSOCK,
                              )
                 ):
                 logger.error( "ZeroMQ request was made against a non-existent device or not using a valid socket [socketB]." )

            if ( e.errno in ( zmq.ETERM,
                              zmq.EMTHREAD,
                              )
                 ):
                 logger.error( "ZeroMQ Context is not in a state to handle this request for [socketB]." )


            cleanUp( aContextINSTANCE, [ socketA, socketB, ] )

  except zmq.ZMQError as e:

            if ( e.errno in ( zmq.EINVAL,
                              zmq.EPROTONOSUPPORT,
                              zmq.ENOCOMPATPROTO,
                              )
                 ):
                 logger.error( "ZeroMQ TransportClass / Endpoint cannot be setup for [socketA]." )

            if ( e.errno in ( zmq.ETERM,
                              zmq.EMTHREAD,
                              )
                 ):
                 logger.error( "ZeroMQ Context is not ready to handle this request for [socketA]." )

            if ( e.errno in ( zmq.ENOTSOCK, ) ):
                 logger.error( "ZeroMQ operation was requested, but not on a valid [socketA]." )

       cleanUp( aContextINSTANCE, [ socketA, ] )

finally:
      # ...
      pass

def cleanUp( aContextToTERMINATE, aListOfSocketsToCLOSE = [] ):
    for      aSocket in aListOfSocketsToCLOSE:
        try:
             aSocket.close()             # external responsibility to setup LINGER as zero right at aSocket instantiation point
        except:
             pass
    try:
         aContextToTERMINATE.term()
    except:
         pass
    finally:
         # ...
         pass

答案 1 :(得分:0)

我不完全确定“哪个插座还活着”是什么意思。 无论connect / bind调用失败,都必须关闭两个套接字。在C libzmq术语中,zmq_close不是zmq_connect / zmq_bind的对应部分,而是zmq_socket的对应部分。 pyzmq中的zmq_socket已由Socket.__init__调用。