我有一个非常简单的MWE-两个WS连接,其中一个将引发错误。两者均作为任务执行,应分别恢复。不幸的是,我的实现导致线程数量的增加(每个错误一个)。我在这里做什么错了?
import websockets
import asyncio
import json
uri = "wss://echo.websocket.org"
async def ws(error):
while True:
try:
async with websockets.connect(uri=uri) as websocket:
await websocket.send("foo")
async for message in websocket:
if error:
raise ValueError
print(message)
except:
print('caught a WS exception - going to sleep for 30 seconds')
websocket.close()
await asyncio.sleep(30)
async def main():
# try:
ws_task_error = loop.create_task(ws(error=True))
ws_task_fine = loop.create_task(ws(False))
await asyncio.wait([ws_task_error, ws_task_fine], return_when=asyncio.FIRST_EXCEPTION)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
据我了解,一旦我离开websockets.connect()的上下文管理器,就应该删除负责线程。我显然在这里没有正确实现它。想法?
-----编辑------
我觉得上面的是WE,但实际上不是M。此示例在连接后立即强制发生异常。在日志中,连接正确关闭。尽管如此,我最终还是在每个连接上创建了一个新线程。
import websockets
import asyncio
import logging
uri = "wss://echo.websocket.org"
logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s: %(message)s')
logger = logging.getLogger('websockets')
logger.setLevel(logging.DEBUG)
async def ws():
while True:
async with websockets.connect(uri=uri) as websocket:
try:
raise ValueError
except:
print('Exception thrown')
print('Exiting...')
async def main():
ws_task_error = loop.create_task(ws())
await asyncio.wait([ws_task_error], return_when=asyncio.FIRST_EXCEPTION)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
输出:
2020-01-21 10:44:04,710 DEBUG websockets.protocol: client - state = CONNECTING
2020-01-21 10:44:04,908 DEBUG websockets.protocol: client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x00000210389E6828>)
2020-01-21 10:44:05,001 DEBUG websockets.protocol: client - state = OPEN
Exception thrown
Exiting...
2020-01-21 10:44:05,003 DEBUG websockets.protocol: client - state = CLOSING
2020-01-21 10:44:05,003 DEBUG websockets.protocol: client > Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:05,098 DEBUG websockets.protocol: client < Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:05,099 DEBUG websockets.protocol: client - event = connection_lost(None)
2020-01-21 10:44:05,100 DEBUG websockets.protocol: client - state = CLOSED
2020-01-21 10:44:05,100 DEBUG websockets.protocol: client x code = 1000, reason = [no reason]
2020-01-21 10:44:05,100 DEBUG websockets.protocol: client x closing TCP connection
2020-01-21 10:44:05,193 DEBUG websockets.protocol: client - state = CONNECTING
2020-01-21 10:44:05,393 DEBUG websockets.protocol: client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x0000021038AB2908>)
2020-01-21 10:44:05,482 DEBUG websockets.protocol: client - state = OPEN
Exception thrown
Exiting...
2020-01-21 10:44:05,489 DEBUG websockets.protocol: client - state = CLOSING
2020-01-21 10:44:05,489 DEBUG websockets.protocol: client > Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:05,580 DEBUG websockets.protocol: client < Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:05,581 DEBUG websockets.protocol: client - event = connection_lost(None)
2020-01-21 10:44:05,582 DEBUG websockets.protocol: client - state = CLOSED
2020-01-21 10:44:05,582 DEBUG websockets.protocol: client x code = 1000, reason = [no reason]
2020-01-21 10:44:05,583 DEBUG websockets.protocol: client x closing TCP connection
2020-01-21 10:44:05,670 DEBUG websockets.protocol: client - state = CONNECTING
2020-01-21 10:44:05,863 DEBUG websockets.protocol: client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x0000021038AB2B70>)
2020-01-21 10:44:05,950 DEBUG websockets.protocol: client - state = OPEN
Exception thrown
Exiting...
2020-01-21 10:44:05,953 DEBUG websockets.protocol: client - state = CLOSING
2020-01-21 10:44:05,953 DEBUG websockets.protocol: client > Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:06,036 DEBUG websockets.protocol: client < Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:06,037 DEBUG websockets.protocol: client - event = connection_lost(None)
2020-01-21 10:44:06,037 DEBUG websockets.protocol: client - state = CLOSED
2020-01-21 10:44:06,038 DEBUG websockets.protocol: client x code = 1000, reason = [no reason]
2020-01-21 10:44:06,039 DEBUG websockets.protocol: client x closing TCP connection
2020-01-21 10:44:06,124 DEBUG websockets.protocol: client - state = CONNECTING
2020-01-21 10:44:06,308 DEBUG websockets.protocol: client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x0000021038AC6C50>)
2020-01-21 10:44:06,400 DEBUG websockets.protocol: client - state = OPEN
Exception thrown
Exiting...
2020-01-21 10:44:06,402 DEBUG websockets.protocol: client - state = CLOSING
2020-01-21 10:44:06,402 DEBUG websockets.protocol: client > Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:06,495 DEBUG websockets.protocol: client < Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:06,496 DEBUG websockets.protocol: client - event = connection_lost(None)
2020-01-21 10:44:06,496 DEBUG websockets.protocol: client - state = CLOSED
2020-01-21 10:44:06,496 DEBUG websockets.protocol: client x code = 1000, reason = [no reason]
2020-01-21 10:44:06,497 DEBUG websockets.protocol: client x closing TCP connection
2020-01-21 10:44:06,600 DEBUG websockets.protocol: client - state = CONNECTING
2020-01-21 10:44:06,827 DEBUG websockets.protocol: client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x0000021038AC6B70>)
2020-01-21 10:44:06,934 DEBUG websockets.protocol: client - state = OPEN
Exception thrown
Exiting...
2020-01-21 10:44:06,936 DEBUG websockets.protocol: client - state = CLOSING
2020-01-21 10:44:06,936 DEBUG websockets.protocol: client > Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:07,042 DEBUG websockets.protocol: client < Frame(fin=True, opcode=8, data=b'\x03\xe8', rsv1=False, rsv2=False, rsv3=False)
2020-01-21 10:44:07,043 DEBUG websockets.protocol: client - event = connection_lost(None)
2020-01-21 10:44:07,044 DEBUG websockets.protocol: client - state = CLOSED
2020-01-21 10:44:07,044 DEBUG websockets.protocol: client x code = 1000, reason = [no reason]
2020-01-21 10:44:07,045 DEBUG websockets.protocol: client x closing TCP connection
答案 0 :(得分:0)
while True:
应该在async with websockets.connect(uri=uri) as websocket:
下
实际上,在您的示例中,ws_task_fine
任务在第一条消息后锁定在async for message in websocket:
中。 ws_task_error
任务循环,增加线程。
这是一个如何实施的粗略例子:
import websockets
import asyncio
uri = "wss://echo.websocket.org"
async def ws(error):
async with websockets.connect(uri=uri) as websocket:
while True:
print('Starting...' + str(error))
try:
await websocket.send("foo")
message = await websocket.recv()
print(message)
if error:
raise ValueError('Exception')
except Exception as e:
print(e)
print('Finishing...' + str(error))
await asyncio.sleep(2)
async def main():
# try:
ws_task_error = loop.create_task(ws(error=True))
# ws_task_fine = loop.create_task(ws(False))
await asyncio.wait([ws_task_error])
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.run_forever()
loop.run_forever()
允许我们在每次完成main()
协程后重新启动
输出:
Starting...True
foo
Exception
Finishing...True
Starting...True
foo
Exception
Finishing...True
Starting...True
foo
Exception
Finishing...True