【问题标题】:RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0' in pythonRuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0' in python
【发布时间】:2021-07-07 11:46:02
【问题描述】:

我正在尝试在 Google Cloud Function 中运行以下 python 脚本。

运行时:Python 3.8

入口点:hello_world

from requests_html import HTMLSession
def hello_world(request):
    session = HTMLSession()
        
    r = session.get('https://translate.google.com')
    r.html.render()  
    app = r.html.find('#yDmH0d')
    for value in app:
        return value.text[:10]

触发事件:{}

当我在谷歌云平台测试这个功能时,我得到了以下错误。

Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, 
    in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, 
    in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, 
    in handle_user_exception reraise(exc_type, exc_value, tb) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, 
    in reraise raise value File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, 
    in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, 
    in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 87, 
    in view_func return function(request._get_current_object()) File "/workspace/main.py", line 6, 
    in hello_world r.html.render() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 586, 
    in render self.browser = self.session.browser # Automatically create a event loop and browser File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 727, 
    in browser self.loop = asyncio.get_event_loop() File "/opt/python3.8/lib/python3.8/asyncio/events.py", line 639, 
    in get_event_loop raise RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.
Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, 
    in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, 
    in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, 
    in handle_user_exception reraise(exc_type, exc_value, tb) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, 
    in reraise raise value File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, 
    in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, 
    in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 87, 
    in view_func return function(request._get_current_object()) File "/workspace/main.py", line 6, 
    in hello_world r.html.render() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 586, 
    in render self.browser = self.session.browser # Automatically create a event loop and browser File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 727,
    in browser self.loop = asyncio.get_event_loop() File "/opt/python3.8/lib/python3.8/asyncio/events.py", line 639, 
    in get_event_loop raise RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.

如何解决这个错误?

【问题讨论】:

    标签: python-3.x web-scraping google-cloud-platform google-cloud-functions python-requests-html


    【解决方案1】:

    Request-HTML 网站上说只支持Python 3.6,因此无法在 Cloud Functions 上运行它。

    在这种情况下,您可能想尝试其他网络爬虫。

    【讨论】:

    • 你好@Akash 这是answer your question吗?
    • 我的本地机器上有 python 3.9,request_html 正在处理它。我在谷歌云平台上尝试了所有 python 选项。它不适用于其中任何一个。你能告诉我为什么它在更高版本的python本地机器上工作吗?
    • @Akash 我明白你的意思,确实它可能适用于更高版本,但不能保证。在这种情况下,Request-HTML 需要 3.6 版本,并且因为 Cloud Functions 在不同的版本 3.8 上运行,所以很遗憾,它解释了为什么这不起作用。
    相关资源