gpt4 book ai didi

python - 为什么 __init__() 缺少参数?

转载 作者:行者123 更新时间:2023-12-01 02:27:46 25 4
gpt4 key购买 nike

摘要

我有一个使用 Websockets 的客户端-服务器应用程序。后端(服务器)部分是使用 autobahn 在 Python 中实现的。

服务器除了提供 Websockets 端点服务之外,还运行一系列线程,通过 queue.Queue() 为 Websockets channel 提供数据。

其中一个线程有问题:它因缺少参数而崩溃,并在解决异常时挂起。

实现细节

服务器实现(删减以突出问题):

from autobahn.asyncio.websocket import WebSocketServerProtocol, WebSocketServerFactory
import time
import threading
import arrow
import queue
import asyncio
import json

# backends of components
import dummy

class MyServerProtocol(WebSocketServerProtocol):
def __init__(self):
super().__init__()
print("webserver initialized")
# global queue to handle updates from modules
self.events = queue.Queue()
# consumer
threading.Thread(target=self.push).start()
threading.Thread(target=dummy.Dummy().dummy, args=(self.events,)).start()

def push(self):
""" consume the content of the queue and push it to the browser """
while True:
update = self.events.get()
print(update)
if update:
self.sendMessage(json.dumps(update).encode('utf-8'), False)
print(update)
time.sleep(1)


def worker(self):
print("started thread")
while True:
try:
self.sendMessage(arrow.now().isoformat().encode('utf-8'), False)
except AttributeError:
print("not connected?")
time.sleep(3)

def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))

def onOpen(self):
print("WebSocket connection open.")

def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))


if __name__ == '__main__':

factory = WebSocketServerFactory(u"ws://127.0.0.1:9100")
factory.protocol = MyServerProtocol

loop = asyncio.get_event_loop()
coro = loop.create_server(factory, '0.0.0.0', 9100)
loop.run_until_complete(coro)
loop.run_forever()

上面代码中导入的dummy模块:

import time
import arrow

class Dummy:
def __init__(self, events):
self.events = events
print("dummy initialized")

def dummy(self):
while True:
self.events.put({
'dummy': {
'time': arrow.now().isoformat()
}
})
time.sleep(1)

问题

当运行上面的代码并从客户端连接时,我得到输出webserver初始化(这证明连接已启动),以及WebSocket连接到'ws://127.0.0.1:9100/'失败:连接建立时出错:客户端上的net::ERR_CONNECTION_REFUSED

调试代码时,我发现对 threading.Thread(target=dummy.Dummy().dummy, args=(self.events,)).start() 的调用崩溃并且调试器 (PyCharm) 将我引导至 C:\Program Files (x86)\Python36-32\Lib\asyncio\selector_events.py,特别是第 236 行

# It's now up to the protocol to handle the connection.
except Exception as exc:
if self._debug:

执行if self._debug时线程挂起,但我在 except行上看到(感谢Pycharm)

exc: __init__() missing 1 required positional argument: 'events'

我的问题

为什么缺少这个参数?它是通过 threading.Thread(target=dummy.Dummy().dummy, args=(self.events,)).start() 调用提供的。

作为一个附带问题:为什么线程会在 if 条件下挂起?

注释

  • 我的程序从来没有抛出回溯(由于挂起)
  • 删除此线程调用可以解决问题(客户端连接正确)

最佳答案

构造函数需要 events 参数,而不是 dummy 方法。我认为你的意思更像是:

d = Dummy(self.events)
threading.Thread(d.dummy).start()

关于python - 为什么 __init__() 缺少参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com