当我在win1上使用loop.run_forever()时,Python3.6无法捕获KeyboardInterrupt

时间:2017-07-30 06:37:11

标签: python-3.x sockets loops python-asyncio keyboardinterrupt

我想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)上停止。 请帮忙。

0 个答案:

没有答案