首页 > 解决方案 > 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)

看起来您的帖子主要是代码;请添加更多详细信息。看起来您的帖子主要是代码;请添加更多详细信息。看起来您的帖子主要是代码;请添加更多细节。

标签: pythonpython-requests

解决方案


推荐阅读