请考虑以下代码:
import asyncio
import pyppeteer
async def hi():
ret = await pyppeteer.launch()
await asyncio.sleep(10)
return ret
browser = asyncio.get_event_loop().run_until_complete(hi())
如果我运行它,然后在几次之后按Ctrl + C,但不到10秒,我会得到一些错误信息,这似乎是一个无限循环:
Traceback (most recent call last):
File "/tmp/a.py", line 10, in <module>
browser = asyncio.get_event_loop().run_until_complete(hi())
File "/usr/lib/python3.6/asyncio/base_events.py", line 454, in run_until_complete
self.run_forever()
File "/usr/lib/python3.6/asyncio/base_events.py", line 421, in run_forever
self._run_once()
File "/usr/lib/python3.6/asyncio/base_events.py", line 1390, in _run_once
event_list = self._selector.select(timeout)
File "/usr/lib/python3.6/selectors.py", line 445, in select
fd_event_list = self._epoll.poll(timeout, max_ev)
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/pyppeteer/launcher.py", line 146, in _close_process
asyncio.get_event_loop().run_until_complete(self.killChrome())
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/pyppeteer/launcher.py", line 146, in _close_process
asyncio.get_event_loop().run_until_complete(self.killChrome())
File "/usr/lib/python3.6/asyncio/base_events.py", line 454, in run_until_complete
self.run_forever()
File "/usr/lib/python3.6/asyncio/base_events.py", line 408, in run_forever
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
Task exception was never retrieved
future: <Task finished coro=<Connection._async_send() done, defined at /home/d33tah/virtualenv-py3/lib/python3.6/site-packages/pyppeteer/connection.py:61> exception=ConnectionClosed('WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason',)>
Traceback (most recent call last):
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/websockets/protocol.py", line 669, in write_frame
yield from self.writer.drain()
File "/usr/lib/python3.6/asyncio/streams.py", line 333, in drain
yield from self._protocol._drain_helper()
File "/usr/lib/python3.6/asyncio/streams.py", line 204, in _drain_helper
raise ConnectionResetError('Connection lost')
ConnectionResetError: Connection lost
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/pyppeteer/connection.py", line 64, in _async_send
await self.connection.send(msg)
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/websockets/protocol.py", line 344, in send
yield from self.write_frame(opcode, data)
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/websockets/protocol.py", line 674, in write_frame
raise ConnectionClosed(self.close_code, self.close_reason)
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason
Task exception was never retrieved
future: <Task finished coro=<Connection._async_send() done, defined at /home/d33tah/virtualenv-py3/lib/python3.6/site-packages/pyppeteer/connection.py:61> exception=ConnectionClosed('WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason',)>
Traceback (most recent call last):
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/websockets/protocol.py", line 669, in write_frame
yield from self.writer.drain()
File "/usr/lib/python3.6/asyncio/streams.py", line 333, in drain
yield from self._protocol._drain_helper()
File "/usr/lib/python3.6/asyncio/streams.py", line 204, in _drain_helper
raise ConnectionResetError('Connection lost')
ConnectionResetError: Connection lost
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/pyppeteer/connection.py", line 64, in _async_send
await self.connection.send(msg)
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/websockets/protocol.py", line 344, in send
yield from self.write_frame(opcode, data)
File "/home/d33tah/virtualenv-py3/lib/python3.6/site-packages/websockets/protocol.py", line 674, in write_frame
raise ConnectionClosed(self.close_code, self.close_reason)
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason
怎么了?我可以看到pyppeteer定义了一个atexit处理程序,为什么它行为不端?