python - RuntimeError:事件循环已关闭“Launcher.killChrome”从未等待,如何解决?
问题描述
当我运行脚本时,它会产生大量的铬,最后不会杀死它。
追溯
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\site-packages\pyppeteer\launcher.py", line 151, in _close_process
self._loop.run_until_complete(self.killChrome())
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 617, in run_until_complete
self._check_closed()
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'Launcher.killChrome' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\site-packages\pyppeteer\launcher.py", line 151, in _close_process
self._loop.run_until_complete(self.killChrome())
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 617, in run_until_complete
self._check_closed()
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\site-packages\pyppeteer\launcher.py", line 151, in _close_process
self._loop.run_until_complete(self.killChrome())
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 617, in run_until_complete
self._check_closed()
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\site-packages\pyppeteer\launcher.py", line 151, in _close_process
self._loop.run_until_complete(self.killChrome())
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 617, in run_until_complete
self._check_closed()
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\site-packages\pyppeteer\launcher.py", line 151, in _close_process
self._loop.run_until_complete(self.killChrome())
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 617, in run_until_complete
self._check_closed()
File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
脚本
from requests_html import AsyncHTMLSession
import asyncio
import json
from itertools import chain
from time import time
async def get_quotes(s, url):
r = await s.get(url)
await r.html.arender()
var_data = r.html.find('script', containing='var data', first=True).text
#this part could be improved, I'm basically isolating the json rendered bit:
*shit, var_data = var_data.split('var data =')
var_data, *shit = var_data.split('; for (var i in data)')
data = json.loads(var_data)
quotes = [post['text'] for post in data]
return quotes
async def main(max_pages=1):
s = AsyncHTMLSession()
tasks = []
for page in range(1,max_pages+1):
url = f'http://quotes.toscrape.com/js/page/{page}'
tasks.append(get_quotes(s,url))
results = await asyncio.gather(*tasks)
return list(chain(*(res for res in results)))
if __name__ == '__main__':
t0 = time()
all_quotes = asyncio.run(main(5))
print(all_quotes)
print(time() - t0)
看起来您的帖子主要是代码;请添加更多详细信息。看起来您的帖子主要是代码;请添加更多详细信息。看起来您的帖子主要是代码;请添加更多细节。
解决方案
推荐阅读
- python-c-api - 从元组解析两个字节时无法访问的内存
- laravel-5.6 - 碳 setTestNow 功能不起作用
- c# - 从装饰属性中提取属性数据
- openssl - 无法使用 OpenSSL 1.1.1 构建 activemq CMS 3.9.5
- javascript - 如何使用正则表达式验证 UPI ID?
- node.js - 使用节点证书的 Google IAP 已过期错误?
- php - 从输入到数组到mysqli表A,并更新表B
- build - 在 Yocto 构建中删除 tmp 时,为什么要删除 sstate-cache?
- android - webview回调在android应用程序中不起作用
- r - 从 2 个不同的数据框中划分 2 列