Skip to content Skip to sidebar Skip to footer

Runtimeerror: There Is No Current Event Loop In Thread 'thread-1'. - Requests_html, Html.render()

I'm trying to render a HTML page in every 10 secs in Python with Requests-HTML module. For the first run it works perfectly, but after it crashes, with the error message below. My

Solution 1:

Instead of starting a timer (and thus a new thread) each time you want to do a request, it would probably be better to just start one thread that does the request every 10 seconds.

For example:

classRequestThread(Thread):def__init__(self):
        super().__init__()
        self.stop = Event()

    defrun(self):
        whilenotself.stop.wait(10):
            session = HTMLSession()
            r = session.get('https://something.com/')
            r.html.render()   

    defstop(self):
         self.stop.set()

However, it seems requests_html is very thread unfriendly (it uses signals among other things). So you must run this in the main thread and create a thread for anything else you want to do. Something like this seems to work:

import requests_html
import time

def get_data_from_page():
    print(time.time())
    session = requests_html.HTMLSession()
    r = session.get('https://google.com')
    r.html.render()

while True:
    next_time = time.time() + 10get_data_from_page()

    wait_for = next_time - time.time()
    if wait_for > 0:
        time.sleep(wait_for)

Post a Comment for "Runtimeerror: There Is No Current Event Loop In Thread 'thread-1'. - Requests_html, Html.render()"