Akash
Akash

Reputation: 77

RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0' in python

I am trying to run below python script in Google Cloud Function.

Runtime : Python 3.8

Entry point : 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]

Triggering event: {}

When I am testing this function in google cloud platform, I am getting following error.

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'.

How to solve this error?

Upvotes: 1

Views: 783

Answers (1)

Farid Shumbar
Farid Shumbar

Reputation: 1420

On the Request-HTML site it says that only Python 3.6 is supported, so it's not possible to run it on Cloud Functions.

You may want to try another web scraper in this case.

Upvotes: 1

Related Questions