Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for non-async scenarios #14

Open
jaredcnance opened this issue Jan 31, 2020 · 4 comments
Open

Better support for non-async scenarios #14

jaredcnance opened this issue Jan 31, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@jaredcnance
Copy link
Member

jaredcnance commented Jan 31, 2020

If you are using this within a ThreadPoolExecutor you may get the following error:

RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.

A workaround for this is to use asyncio.run instead. However, this has some overhead and it would be better if we provided a synchronous mechanism. @metric_scope can check whether or not an event loop exists, and if not, it can fall back to using a synchronous logger implementation.

This will likely be superseded by #21

@jbarz1
Copy link

jbarz1 commented Apr 12, 2021

Just curious when you think this will be resolved

@grapek9
Copy link

grapek9 commented Feb 3, 2022

Workaround :
In file aws_embedded_metrics/metric_scope/init.py

Change in line 49
from : loop = asyncio.get_event_loop()
to : loop = asyncio.new_event_loop()

So instead of looking for current event loop metrics create its own.
It's not a solution, just workaround.

@jaredcnance
Copy link
Member Author

jaredcnance commented Aug 10, 2022

If you're hitting this scenario then you are likely doing some explicit multi-threading since get_event_loop will create a new thread unless you're already running on a background thread (see here).

The solution above (creating a new event loop per logger) can run you into race conditions if you try to create multiple loggers on the same thread. It also has a lot of overhead by creating an event loop for each logger. Rather than doing that, you probably would want to create a single event loop for the app and run your tasks inside of that event loop rather than doing explicit multi-threading (see this example; alternatively you can use loop.run_in_executor() or asyncio.to_thread()). If you really need to do explicit multi-threading outside of an async runtime then you could create a loop per thread, though this has some overhead and would need to be done exactly once per thread.

@SamStephens
Copy link

SamStephens commented Sep 14, 2023

Is this still planned? It's causing me all kinds of pain trying to use this package with Flask, Gunicorn and Gevent where for reasons I don't fully understand I cannot use Flask's async support. I don't want to be creating event loops per thread, so I'm using this hack to make MetricsLogger synchronous.

Fundamentally I don't think it's really okay for a core library like this to require async support, as in Python it's possible to be in execution contexts where you don't have access to the event loop on the main thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants