requests-html icon indicating copy to clipboard operation
requests-html copied to clipboard

RuntimeError: There is no current event loop in thread 'Dummy-5'.

Open thisiseddy-ab opened this issue 2 years ago • 3 comments

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 avatar Aug 17 '22 16:08 thisiseddy-ab

@thisiseddy-ab as suggested by the RuntimeError, use AsyncHTMLSession() instead of HTMLSession()

Spy0x7 avatar Aug 20 '22 02:08 Spy0x7

@thisiseddy-ab as suggested by the RuntimeError, use AsyncHTMLSession() instead of HTMLSession()

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

thisiseddy-ab avatar Aug 20 '22 09:08 thisiseddy-ab

Any updates? encountering the same issue

margaritageleta avatar Dec 27 '23 21:12 margaritageleta