-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Sharing the loop with other libraries #152
Comments
My 2 cents on this: starting with Python 3.6 and 3.5.3, it will be recommended to never pass the loop explicitly. |
Instead of |
@masnun How should we go about knowing if the user isn't using the |
Hi, we should be able to use this function - https://docs.python.org/3/library/asyncio-eventloops.html#asyncio.get_event_loop_policy to do so? I didn't try it out, a quick google got me there. |
Taken care of in sanic 0.2.0 I think. Closing for now! |
Don't think it's fixed, but ok. |
I'll reopen then @masnun |
Thanks. I will try to come up with some ideas or perhaps a PR. Let's see. |
I'm having some problems/confusion around mixing sanic Passing in an both explicit loop (asyncio / uvloop) and specifying multiple wokers e.g.:
However, now, omitting the
I'm attempting to create a simple video thumbnail service using Both of these issues can be easily reproduced using the existing aiohttp_example.py and either omitting the Anyway, its exciting to see python beginning to catch up a little in the concurrent services world! Thanks, |
@ubergarm that example was incorrect, I'm updating it. Below is the updated version (which works). With multiple workers (at least) it doesn't seem that passing the event loop is the correct way.
|
Note that we no longer pass the loop to |
@masnun based on the above comment by @1st1, I think the problem here is actually twofold:
|
This seems to be the correct approach. |
Thanks for clearing this up, things are moving fast! I still wanted a way to limit the maximum concurrency and the way I came up with to get the Is there a cleaner or preferred way to do something like this yet?
|
The |
If the code is executed before you call the One solution is to use |
I'm not sure it is even necessary to call http://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession (read about the loop param a few lines down) So you can go from this:
to just this:
and it should be the same I think. |
I think this should be closed per #348 |
#348 Won't really work until https://github.com/channelcat/sanic/blob/master/sanic/server.py#L301 is removed. What if user called asyncio.get_event_loop() before sanic set it? Or wants to explicitly set uvloop by himself instead of relying on some magic that happens in third party library. This concept of library changing global settings of asyncio seems very wrong and dangerous to me. It's nice you want sanic to be fast and it is fastest with uvloop, but it should be resolved by documentation and not magic. |
@JendaPlhak which is why we recommend that users (if they need to share the event loop that gets created by sanic) use the Sorry if that wasn't clear through the documentation. I agree that we shouldn't be setting the global loop like that as well. |
Ok, and are you going to change that? (Since usage of mentioned arguments is fairly inconvenient) |
Sharing a global loop, that's instantiated before a sanic application is run is, in my opinion, an anti-pattern. Going to close this again, will reopen if necessary. |
@1st1 sorry, late to this party. But can you clarify what you mean by:
Do you mean passing the loop specifically in the context of coroutines? Or do you mean to workers, etc? How else are we to add tasks to a loop from an executor? |
Whenever you're in async/await context, i.e. in the body of a coroutine, it's safe to call If, however, you're writing some callback based code (read |
Hey , i want use before_start and after_start but they are no more in the args of the run function what is the best way to do ? thank you
|
If we don't pass a loop to the
run
method, it creates a new loop and sets it as the event loop for asyncio.But if any other libraries (for example motor) run any code involving the event loop, before we call the
run
method, they are likely to get the default event loop from asyncio before the above code executes. So they would be running on a separate event loop and my server would be on another event loop.The obvious solution is to create an uvloop ourselves and passing it to run method. Or deferring any event loop related codes until the server starts (may
await
from otherasync
functions inside views). We can also choose useget_event_loop
method. If uvloop is available and theget_event_loop
is not using uvloop, we display a warning.Or we can simply add a detailed note somewhere as part of the documentation.
The text was updated successfully, but these errors were encountered: