faust-streaming / mode

Python AsyncIO Services
https://faust-streaming.github.io/mode/
Other
42 stars 16 forks source link

fix: service class trying to get event loop on init #62

Closed hassanselim0 closed 5 months ago

hassanselim0 commented 5 months ago

The _new_*_event methods on the Service class were calling the loop property instead of using the _loop attribute, the property tried to fetch the current thread's event loop which caused some odd issues, this fix delays that until the event loop is actually needed (lazy loading).

This fixes #61

hassanselim0 commented 5 months ago

I hope this makes its way to a hotfix release soon-ish because currently I have this ugly workaround code that runs for every gunicorn thread:

try:
    asyncio.get_event_loop()
except RuntimeError as e:
    if str(e).startswith('There is no current event loop in thread'):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
    else:
        raise

I'm basically creating an event loop for each thread even though I don't need one, just so things don't break when I try to construct a faust app object.

wbarnha commented 5 months ago

On further review, I agree that lazy loading is the right thing to do. I need to see if there's a regression test that covers the issue I originally attempted to patch in https://github.com/faust-streaming/mode/issues/38.

wbarnha commented 5 months ago

I'll push a release out tomorrow. I try to stay out of the habit of releasing changes on weekends.