【发布时间】:2021-05-30 23:14:45
【问题描述】:
我无法理解这个由这里的代码块生成的错误:
def websock():
while True:
async def WebSocketserver(websocket, path):
global message
global logmsg
while True:
Rxdata = await websocket.recv()
# construct the message that we will log
data = json.loads(Rxdata)
command = data.get('Message')
person = data.get('Name')
commandTime = data.get('Time')
message = command
logmsg = [person, message, str(commandTime), "allowed", "console"]
print(f" {person} on the controller: {command}")
await websocket.send("200")
start_server = websockets.serve(WebSocketserver, Host, SocketPort)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
这给了我以下错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\twitch\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\envs\twitch\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "D:\CloudStorage\OneDrive - Personal\OneDrive\Projects-R2D2\scripts-twitchPlays\code\TwitchPlays_2\TwitchPlaysCode.py", line 276, in websock
start_server = websockets.serve(WebSocketserver, Host, SocketPort)
File "C:\ProgramData\Anaconda3\envs\twitch\lib\site-packages\websockets\legacy\server.py", line 999, in __init__
loop = asyncio.get_event_loop()
File "C:\ProgramData\Anaconda3\envs\twitch\lib\asyncio\events.py", line 642, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1'.
没有事件循环是什么意思? while 语句不应该涵盖这一点吗?我从这里借用这个理解:https://medium.com/@tigranbs/concurrency-vs-event-loop-vs-event-loop-concurrency-eb542ad4067b 但我现在很迷茫。
【问题讨论】:
标签: python websocket python-asyncio