将aiozmq.rpc与pyqt5一起使用时出现问题

2024-04-29 02:36:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用PyQt5中的客户机-服务器模型进行RPC

我已经修改了pyqt5的客户机示例代码,这是原始代码(https://gist.github.com/martinku-tw/a35493bb40d3a91632a276492402a3ab),但是我在尝试将aiozmq.rpc与pyqt5一起使用时遇到了一个问题

当我试图从rpc获取返回值时,我得到一个错误“TypeError:必须是str,而不是coroutine”

我对rpc/socket编程非常陌生,我不确定在PyQt5中这样做是否正确

任何帮助都将不胜感激

我一步一步地浏览代码并查看ret对象,但没有看到返回值。从我所看到的,我不认为它击中了服务器代码


from PyQt5.QtWidgets import *
import asyncio
from aiozmq import rpc

app = QApplication([])
button = QPushButton('Click to myfunc1 results')

@asyncio.coroutine
async def run_client():
    print('start rpc client...')
    client = await rpc.connect_rpc(connect='tcp://127.0.0.1:5503')
    ret = await client.call.myfunc1(1, 13)

    return ret

def on_button_clicked():

    #ret = run_client().cr_code.co_consts[5]
    ret = run_client()

    alert = QMessageBox()
    alert.setText('myfunc1 results is: ' + str(ret))
    alert.exec_()

button.clicked.connect(on_button_clicked)
button.show()
app.exec_()

我期望从服务器返回字符串对象,但收到此错误

Traceback (most recent call last):
  File "aiozmq_client_qt_1.py", line 22, in on_button_clicked
    alert.setText('myfunc1 results is: ' + ret)
TypeError: must be str, not coroutine
[1]    6689 abort      python aiozmq_client_qt_1.py

Tags: run代码importclientbuttonalertrpcresults