Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何停止发布套接字的无限等待ZMQ recv函数?_Python 2.7_Sockets_Pyzmq - Fatal编程技术网

Python 2.7 如何停止发布套接字的无限等待ZMQ recv函数?

Python 2.7 如何停止发布套接字的无限等待ZMQ recv函数?,python-2.7,sockets,pyzmq,Python 2.7,Sockets,Pyzmq,从ZMQ套接字接收消息时遇到问题。我创建了一个套接字,将其绑定到一个地址并设置其订阅。但是,当我尝试从套接字接收消息时,程序挂起 self.context = zmq.Context() self.pub_socket = self.context.socket(zmq.SUB) self.pub_socket.connect(pub_socket_addr) self.pub_socket.setsockopt(zmq.SUBSCRIBE, value) reply = self.pub_so

从ZMQ套接字接收消息时遇到问题。我创建了一个套接字,将其绑定到一个地址并设置其订阅。但是,当我尝试从套接字接收消息时,程序挂起

self.context = zmq.Context()
self.pub_socket = self.context.socket(zmq.SUB)
self.pub_socket.connect(pub_socket_addr)
self.pub_socket.setsockopt(zmq.SUBSCRIBE, value)
reply = self.pub_socket.recv()
我们尝试在无限循环中捕获带有NOBLOCK标志的ZMQError,如以下代码所示,但它也不会从服务器接收任何数据

while True:
        try:
            reply = self.pub_socket.recv(zmq.NOBLOCK)
            try:
                if(json.loads(reply)[command.Command.COMM_TYPE] == command.Command.GAME_START):
                    return True
            except ValueError:
                continue
        except zmq.ZMQError:
            break

可能会出现什么问题?

您的第一个代码段会挂起,因为
recv()
仅在收到后返回

无限循环在第一次迭代时被
中断
ing,因为
zmq.ZMQError
异常会上升,因为没有可接收的内容。您可以尝试将
break
指令替换为
time.sleep(0.1)
以进行迭代,直到套接字接收到某个内容并且满足
GAMESTART
条件

下面的代码应该可以做到这一点:

got_game_start = False
while not got_game_start:
    try:
        reply = self.pub_socket.recv(zmq.NOBLOCK)
    except zmq.ZMQError:
        time.sleep(0.1)
    else:
        try:
            got_game_start = (json.loads(reply)[command.Command.COMM_TYPE] == command.Command.GAME_START)
        except:
            got_game_start = False
# at this point the loop should end after receiving the game start message

您的第一个代码段挂起,因为
recv()
仅在收到后返回

无限循环在第一次迭代时被
中断
ing,因为
zmq.ZMQError
异常会上升,因为没有可接收的内容。您可以尝试将
break
指令替换为
time.sleep(0.1)
以进行迭代,直到套接字接收到某个内容并且满足
GAMESTART
条件

下面的代码应该可以做到这一点:

got_game_start = False
while not got_game_start:
    try:
        reply = self.pub_socket.recv(zmq.NOBLOCK)
    except zmq.ZMQError:
        time.sleep(0.1)
    else:
        try:
            got_game_start = (json.loads(reply)[command.Command.COMM_TYPE] == command.Command.GAME_START)
        except:
            got_game_start = False
# at this point the loop should end after receiving the game start message

请提供一个更完整的示例,包括最小服务器代码和“订阅”套接字选项调用的实际值请提供一个更完整的示例,包括最小服务器代码和“订阅”套接字选项调用的实际值callI做了与您所说完全相同的事情,但是它仍然停留在循环中。但是如果服务器没有发送任何内容,那么我也无法接收任何内容。所以我想我应该检查服务器是否正在向我发送消息。我做了和你说的完全相同的事情,但它仍然卡在循环中。但是如果服务器没有发送任何消息,那么我也无法接收任何消息。所以我想我应该检查服务器是否正在向我发送消息。