首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pyzmq:套接字中断使用"ZMQError:操作无法在当前状态下完成“

pyzmq:套接字中断使用"ZMQError:操作无法在当前状态下完成“
EN

Stack Overflow用户
提问于 2020-05-18 16:18:42
回答 1查看 1.6K关注 0票数 2

我对ZeroMQ非常陌生,并试图构建一个非常基本的消息传递系统。这段代码很大程度上是基于这里的例子,有些曲折。

由于某种原因,在最后一条消息到达frontend套接字(hbmtx)后,代码抛出错误,我不确定它的来源

抱歉,代码/输出太长了,我觉得很重要的一点是要强调问题所在,并帮助调试。

下面是我的代码+输出:

代码语言:javascript
运行
复制
s_frontend = "ipc://frontend.ipc"
s_backend  = "ipc://backend.ipc"

import time

def hbm():
    """Worker task, using a REQ socket to do load-balancing."""
    socket = zmq.Context().socket(zmq.REQ)
    socket.identity = u"hbm".encode("ascii")
    socket.connect(s_frontend)
    # Tell broker we're ready for work
    socket.send(b"READY")
    while True:
        msgs = socket.recv_multipart()
        print("hbm got something", msgs)
        if "BE READY" in msgs:
            print("hbm:BE is ready for some work")
        time.sleep(3)

def tx():
    """Worker task, using a REQ socket to do load-balancing."""
    socket = zmq.Context().socket(zmq.REQ)
    socket.identity = u"txtx".encode("ascii")
    socket.connect(s_frontend)
    # Tell broker we're ready for work
    socket.send(b"READY")
    while True:
        msgs = socket.recv_multipart()
        print("tx got something ", msgs)
        if "BE READY" in msgs:
            print("tx:BE is ready for some work")
        time.sleep(3)


def dev():
    """Worker task, using a REQ socket to do load-balancing."""
    socket = zmq.Context().socket(zmq.REQ)
    socket.identity = u"dev".encode("ascii")
    socket.connect(s_backend)
    # Tell broker we're ready for work
    socket.send(b"READY")


def main():
    # Prepare context and sockets
    context = zmq.Context.instance()
    frontend = context.socket(zmq.ROUTER)
    frontend.bind( s_frontend )                 # ( "ipc://frontend.ipc" )
    backend = context.socket(zmq.ROUTER)
    backend.bind( s_backend )                   # ( "ipc://backend.ipc" )

    def start(task, *args):
        process = multiprocessing.Process(target=task, args=args)
        process.daemon = True
        process.start()

    start(hbm)
    start(tx)


    time.sleep(1)
    start(dev)

    clients = []
    poller = zmq.Poller()
    poller.register(backend, zmq.POLLIN)
    #poller.register(frontend, zmq.POLLIN)
    all_is_ready = False
    while True:
        sockets = dict(poller.poll(timeout=1))
        #print(sockets)
        soc = None
        if backend in sockets:
            print("got something from backend")
            msg = backend.recv_multipart()
            print(msg)
            print("adding frontend to poller")
            poller.register(frontend, zmq.POLLIN)
            print("backend is ready, notify frontend")

        elif frontend in sockets:
            print("got something from frontend")
            msg = frontend.recv_multipart()
            print(msg)
            clients.append(bytes(msg[0]))

        elif len(clients) == 2 and all_is_ready is False:
            all_is_ready = True
            for c in clients:
                print("sending response to", c)
                time.sleep(0.1) # just to prevent print overlap
                frontend.send_multipart([c, b"", b"BE READY"])
        else:
            print("so much work, no rest, sleeping for 3")
            time.sleep(3)


    # Clean up
    backend.close()
    frontend.close()
    context.term()


if __name__ == "__main__":
    main()

运行此代码将产生以下输出:

代码语言:javascript
运行
复制
so much work, no rest, sleeping for 3
got something from backend
['dev', '', 'READY']
adding frontend to poller
backend is ready, notify frontend
got something from frontend
['hbm', '', 'READY']
got something from frontend
['txtx', '', 'READY']
sending response to hbm
sending response to txtx
hbm got something ['BE READY']
hbm:BE is ready for some work
tx got something  ['BE READY']
tx:BE is ready for some work
so much work, no rest, sleeping for 3
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/vm/repo/pyzmq_examples/hal_example/example1.py", line 104, in hbm
    msgs = socket.recv_multipart()
  File "/usr/local/lib/python2.7/dist-packages/zmq/sugar/socket.py", line 475, in recv_multipart
    parts = [self.recv(flags, copy=copy, track=track)]
  File "zmq/backend/cython/socket.pyx", line 791, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 827, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 191, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/socket.pyx", line 186, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
    raise ZMQError(errno)
ZMQError: Operation cannot be accomplished in current state
so much work, no rest, sleeping for 3
Process Process-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/vm/repo/pyzmq_examples/hal_example/example1.py", line 130, in tx
    msgs = socket.recv_multipart()
  File "/usr/local/lib/python2.7/dist-packages/zmq/sugar/socket.py", line 475, in recv_multipart
    parts = [self.recv(flags, copy=copy, track=track)]
  File "zmq/backend/cython/socket.pyx", line 791, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 827, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 191, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/socket.pyx", line 186, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
    raise ZMQError(errno)
ZMQError: Operation cannot be accomplished in current state
so much work, no rest, sleeping for 3
Traceback (most recent call last):
  File "/home/vm/repo/pyzmq_examples/hal_example/example1.py", line 244, in <module>
    main()
  File "/home/vm/repo/pyzmq_examples/hal_example/example1.py", line 233, in main
    time.sleep(3)
KeyboardInterrupt

进程已完成,退出代码为%1

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-18 16:36:14

Q:“.抛错.我不确定它的来源。”

代码语言:javascript
运行
复制
ZMQError: Operation cannot be accomplished in current state

在使用REQ-Archetype的每个用例中,代码都不符合本机API条件:

代码语言:javascript
运行
复制
...
socket = zmq.Context().socket( zmq.REQ ) #--------------------- REQ socket Archetype
socket.identity = u"hbm".encode( "ascii" )
socket.connect( s_frontend )
socket.send( b"READY" ) #-------------------------------------- REQ.send()-s
while True:             # ..................................... REQ next can .recv()
    #      socket-FSA( of a type of REQ ) can execute a .recv() iff.send() preceded.....?
    msgs = socket.recv_multipart() #- - - - - - - - - - - - - - REQ.recv_multipart()-s
    #.......................................................... REQ next can .send()
    continue                       #-?-?-?-?-?-?: DID IT TRY TO REQ.send()? NO, NEVER...!

您的意思是REQ必须执行迭代的recv发送,而不能做任何其他的方式吗?(发送recv) - LordTitiKaka

REQ-Archetype对被保留的强制排序有一种根深蒂固的期望:

.send() - .recv() .无穷大

任何违反此翻转/触发器更改其内部有限状态自动机(FSA)内部状态的行为都将导致上述ZMQError: Operation cannot be accomplished in current state

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61874154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档