我想tcpserver
使用asyncio
。首先我使用:
loop = asyncio.get_event_loop()
server = asyncio.start_server(process, host="0.0.0.0", port=8888, loop=loop)
loop.run_until_complete(Server)
但我发现loop.run_until_complete
会立即返回,所以我打电话给loop.run_forever()
来阻止它。然后它无法捕捉KeyboardInterrupt
。
import time, asyncio, socket, sys
async def process(self, reader, writer):
await asyncio.sleep(1)
try:
loop = asyncio.get_event_loop()
server = asyncio.start_server(process, host="0.0.0.0", port=8888, loop=Loop)
loop.run_until_complete(server)
loop.run_forever()
except KeyboardInterrupt as e:
loop.stop()
loop.close()
此代码在linux(python3.6)上正常工作,但不能在win10 15063(python3.6)上停止。 请帮忙。