Python GUI 未捕获 zeromq 消息
Python GUI not capturing zeromq message
我有一个小程序,我没有收到服务器的响应。
这是在 python 3.4 和 Ubuntu 14.04.
上使用最新的 zeromq 和 pyqt5
客户端 GUI 向服务器发送一条消息,服务器获取并响应但客户端看不到响应。
代码如下:
miniServer.py:
import os
import sys
import time
import zmq
from zmq.eventloop import ioloop
from zmq.eventloop.zmqstream import ZMQStream
# Prepare for client socket
ctx = zmq.Context.instance()
sIn = ctx.socket(zmq.PULL)
urlIn = 'tcp://127.0.0.1:1234'
sIn.bind(urlIn)
# Prepare for sockets to GUI and Web
guiCalled = False
webCalled = False
urlGui = 'tcp://127.0.0.1:2345'
urlWeb = 'tcp://127.0.0.1:3456'
sGUI = None
sWeb = None
def __GetConfig__(sender, data):
if "GUI" == sender:
print("Sending back config list to GUI")
sGUI.send_string("From Server to GUI")
elif "WEB" == sender:
sWeb.send_string("From Server to Web")
def __CheckGUICalled__():
# Used to only connnect once
global guiCalled
global sGUI
if not guiCalled:
print("Connected to client GUI at port 2345")
guiCalled = True
sGUI = ctx.socket(zmq.PUSH)
sGUI.connect(urlGui)
def __CheckWebCalled__():
# Used to only connnect once
global webCalled
global sWeb
if not webCalled:
webCalled = True
sWeb = ctx.socket(zmq.PUSH)
sWeb.connect(urlWeb)
actions = {
"GET_CONFIG": __GetConfig__}
clients = {
"GUI": __CheckGUICalled__,
"WEB": __CheckWebCalled__}
def check_msg(msg):
newStr = msg[0].decode("utf-8", "strict")
if newStr.count(":") == 2:
[sender, command, data] = newStr.split(":")
print("Sender: " + sender + ", Command: " + command + ", Data: " + data)
# connect if not already done
clients.get(sender, lambda: None)()
# execute the command sent from client
actions.get(command, lambda: None)(sender, data)
# register the check_msg callback to be fired
# whenever there is a message on our socket
stream = ZMQStream(sIn)
stream.on_recv(check_msg)
# Setup callback handling the XMOS
tic = time.time()
def xmos_handler():
# just testing
print("Loop time: %.3f" % (time.time() - tic))
pc = ioloop.PeriodicCallback(xmos_handler, 200)
pc.start()
# start the eventloop
ioloop.IOLoop.instance().start()
miniGUI.py:
import os
import sys
import zmq
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from zmq.eventloop.zmqstream import ZMQStream
# prepare out socket to server
ctx = zmq.Context.instance()
sOut = ctx.socket(zmq.PUSH)
sOut.connect('tcp://127.0.0.1:1234')
# handle inputs from server
def check_msg(msg):
newStr = msg[0].decode("utf-8", "strict")
print("Message: " + newStr + " received")
sIn = ctx.socket(zmq.PULL)
sIn.bind('tcp://127.0.0.1:2345')
stream = ZMQStream(sIn)
stream.on_recv(check_msg)
# setup window
class Form(QWidget):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
nameLabel = QLabel("Name:")
self.nameLine = QLineEdit()
self.submitButton = QPushButton("Submit")
buttonLayout1 = QVBoxLayout()
buttonLayout1.addWidget(nameLabel)
buttonLayout1.addWidget(self.nameLine)
buttonLayout1.addWidget(self.submitButton)
self.submitButton.clicked.connect(self.submitContact)
mainLayout = QGridLayout()
mainLayout.addLayout(buttonLayout1, 0, 1)
self.setLayout(mainLayout)
self.setWindowTitle("For Test: GUI:GET_CONFIG:0")
def submitContact(self):
name = self.nameLine.text()
sOut.send_string(name)
print("Message " + name + " sent")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
screen = Form()
screen.show()
sys.exit(app.exec_())
在一个终端中首先启动服务器:
python3.4 miniServer.py
然后是 GUI:
python3.4 miniGUI.py
正在编辑小部件中写入字符串:
GUI:GET_CONFIG:0
并按下提交按钮将在服务器控制台上打印:
Sender: GUI, Command: GET_CONFIG, Data: 0
Connected to client GUI at port 2345
Sending back config list to GUI
并且在 GUI 控制台上只会
Message GUI:GET_CONFIG:0 sent
被写入而预期的结果应该是:
Message GUI:GET_CONFIG:0 sent
Message: From Server to GUI received
我做错了什么?
解决方案是使用套接字类型 REQ/REP 而不是 PUSH/PULL,这样代码也更简洁。
我有一个小程序,我没有收到服务器的响应。
这是在 python 3.4 和 Ubuntu 14.04.
上使用最新的 zeromq 和 pyqt5
客户端 GUI 向服务器发送一条消息,服务器获取并响应但客户端看不到响应。
代码如下:
miniServer.py:
import os
import sys
import time
import zmq
from zmq.eventloop import ioloop
from zmq.eventloop.zmqstream import ZMQStream
# Prepare for client socket
ctx = zmq.Context.instance()
sIn = ctx.socket(zmq.PULL)
urlIn = 'tcp://127.0.0.1:1234'
sIn.bind(urlIn)
# Prepare for sockets to GUI and Web
guiCalled = False
webCalled = False
urlGui = 'tcp://127.0.0.1:2345'
urlWeb = 'tcp://127.0.0.1:3456'
sGUI = None
sWeb = None
def __GetConfig__(sender, data):
if "GUI" == sender:
print("Sending back config list to GUI")
sGUI.send_string("From Server to GUI")
elif "WEB" == sender:
sWeb.send_string("From Server to Web")
def __CheckGUICalled__():
# Used to only connnect once
global guiCalled
global sGUI
if not guiCalled:
print("Connected to client GUI at port 2345")
guiCalled = True
sGUI = ctx.socket(zmq.PUSH)
sGUI.connect(urlGui)
def __CheckWebCalled__():
# Used to only connnect once
global webCalled
global sWeb
if not webCalled:
webCalled = True
sWeb = ctx.socket(zmq.PUSH)
sWeb.connect(urlWeb)
actions = {
"GET_CONFIG": __GetConfig__}
clients = {
"GUI": __CheckGUICalled__,
"WEB": __CheckWebCalled__}
def check_msg(msg):
newStr = msg[0].decode("utf-8", "strict")
if newStr.count(":") == 2:
[sender, command, data] = newStr.split(":")
print("Sender: " + sender + ", Command: " + command + ", Data: " + data)
# connect if not already done
clients.get(sender, lambda: None)()
# execute the command sent from client
actions.get(command, lambda: None)(sender, data)
# register the check_msg callback to be fired
# whenever there is a message on our socket
stream = ZMQStream(sIn)
stream.on_recv(check_msg)
# Setup callback handling the XMOS
tic = time.time()
def xmos_handler():
# just testing
print("Loop time: %.3f" % (time.time() - tic))
pc = ioloop.PeriodicCallback(xmos_handler, 200)
pc.start()
# start the eventloop
ioloop.IOLoop.instance().start()
miniGUI.py:
import os
import sys
import zmq
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from zmq.eventloop.zmqstream import ZMQStream
# prepare out socket to server
ctx = zmq.Context.instance()
sOut = ctx.socket(zmq.PUSH)
sOut.connect('tcp://127.0.0.1:1234')
# handle inputs from server
def check_msg(msg):
newStr = msg[0].decode("utf-8", "strict")
print("Message: " + newStr + " received")
sIn = ctx.socket(zmq.PULL)
sIn.bind('tcp://127.0.0.1:2345')
stream = ZMQStream(sIn)
stream.on_recv(check_msg)
# setup window
class Form(QWidget):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
nameLabel = QLabel("Name:")
self.nameLine = QLineEdit()
self.submitButton = QPushButton("Submit")
buttonLayout1 = QVBoxLayout()
buttonLayout1.addWidget(nameLabel)
buttonLayout1.addWidget(self.nameLine)
buttonLayout1.addWidget(self.submitButton)
self.submitButton.clicked.connect(self.submitContact)
mainLayout = QGridLayout()
mainLayout.addLayout(buttonLayout1, 0, 1)
self.setLayout(mainLayout)
self.setWindowTitle("For Test: GUI:GET_CONFIG:0")
def submitContact(self):
name = self.nameLine.text()
sOut.send_string(name)
print("Message " + name + " sent")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
screen = Form()
screen.show()
sys.exit(app.exec_())
在一个终端中首先启动服务器:
python3.4 miniServer.py
然后是 GUI:
python3.4 miniGUI.py
正在编辑小部件中写入字符串:
GUI:GET_CONFIG:0
并按下提交按钮将在服务器控制台上打印:
Sender: GUI, Command: GET_CONFIG, Data: 0
Connected to client GUI at port 2345
Sending back config list to GUI
并且在 GUI 控制台上只会
Message GUI:GET_CONFIG:0 sent
被写入而预期的结果应该是:
Message GUI:GET_CONFIG:0 sent
Message: From Server to GUI received
我做错了什么?
解决方案是使用套接字类型 REQ/REP 而不是 PUSH/PULL,这样代码也更简洁。