Reputation: 43
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 partial code is:
def get_data_from_page():
session = HTMLSession()
r = session.get('https://something.com/')
threading.Timer(10.0, get_data_from_page).start()
r.html.render()
#code continues
def main():
get_data_from_page()
if __name__ == '__main__':
main()
Error message is:
Exception in thread Thread-1:
File "/home/david/.local/lib/python3.6/site-packages/requests_html.py", line 572, in render
self.session.browser # Automatycally create a event loop and browser
File "/home/david/.local/lib/python3.6/site-packages/requests_html.py", line 679, in browser
self.loop = asyncio.get_event_loop()
File "/usr/lib/python3.6/asyncio/events.py", line 694, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.6/asyncio/events.py", line 602, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-1'.
Upvotes: 4
Views: 3187
Reputation: 271
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:
class RequestThread(Thread):
def __init__(self):
super().__init__()
self.stop = Event()
def run(self):
while not self.stop.wait(10):
session = HTMLSession()
r = session.get('https://something.com/')
r.html.render()
def stop(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() + 10
get_data_from_page()
wait_for = next_time - time.time()
if wait_for > 0:
time.sleep(wait_for)
Upvotes: 1