requests-html
requests-html copied to clipboard
RuntimeError: There is no current event loop in thread 'Dummy-5'.
Im using requests-html inside Celery task but i get this error "RuntimeError: There is no current event loop in thread 'Dummy-5'."
I fIx it like this
class HTMLSession(BaseSession):
def __init__(self, **kwargs):
super(HTMLSession, self).__init__(**kwargs)
@property
def browser(self):
if not hasattr(self, "_browser"):
try:
self.loop = asyncio.get_event_loop()
except RuntimeError as ex:
if "There is no current event loop in thread" in str(ex):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.loop = asyncio.get_event_loop()
if self.loop.is_running():
raise RuntimeError("Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.")
self._browser = self.loop.run_until_complete(super().browser)
return self._browser
it works fine but, im having problem closing celery worker, it cant be because im using celery worker with gevents ? does someone has an idea how gevents and asyncio work
@thisiseddy-ab as suggested by the RuntimeError
, use AsyncHTMLSession()
instead of HTMLSession()
@thisiseddy-ab as suggested by the
RuntimeError
, useAsyncHTMLSession()
instead ofHTMLSession()
I tried with AsyncHTMLSession() i get the same error, i think it is because celery usues gevents that is what causes the problem. Just the record with this fix, it works but i have trouble than closing celery worker, i dont really understand asyncio or gevents to know what causing in it
Any updates? encountering the same issue