-
Notifications
You must be signed in to change notification settings - Fork 270
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
Have to set current loop myself since Daphne 2.4.1 #299
Comments
OK, so... first of all it's a bug fix. It's turns out that just calling Having enquired from the Python core team on this, we should be using
The long and short of it seemed to be "we would deprecate Which isn't perfect I know, but, I don't think calling SO, can I ask, what are you trying to do, and would |
OK, so it looks like the adjustment is still in play here. Via https://bugs.python.org/issue36373, deprecating the loop argument, we have this comment of a PR:
So, in the short run you can use |
|
We can't call this until we're in the thread we're going to use the event loop in, the thread Twisted is running in. (Otherwise we end up accessing the same loop from two threads, which isn't allowed.) IF you've only got one thread in play, or you know you're already in the correct thread (already in a coroutine running under Daphne) then you can call
Grrr. This stuff is moving fast still. |
I want to say thanks for looking into this! I want to raise the question again. Does it make sense to force users of daphne through Django/Starlette/... to have to import the lock and then specify it when using Even, since this pull request: aio-libs/aiohttp#3580 you are no longer allowed to specify a loop as argument to Would it be possible to somewhere in daphne set the event loop with |
Right, but see aio-libs/aiohttp#3331 — there it's explicitly stated that
I'll have to think about it. It's a tricky one. |
(If you fancied implementing #264, that would give a good place to add the call at least...) |
Hi @Matbj — Long time, but now PY36 is end-of-life the prospect here looks slightly clearer...
This is no longer an issue, and The issue here is doing things — like creating the lock — before we spin up the We already call Line 139 in 71ba440
Anything you do after that point will get the right event loop. Anything you do before that will need to set the As such, I'm going to close this as wontfix. I think as asyncio has moved on it's largely become moot. (There are better higher-level APIs available now.) But in any case I think it's easier to address specific cases — saying Defer doing this until ... rather than thinking we can just set the twisted event loop on the main thread at startup, which we already know we can't. Hope that makes sense. Sorry for the slow resolution, but really there wasn't much to be done until Thanks again. |
Environment
Python 3.6
Problem
Usage of any function that relies on default loop - gotten by running
asyncio.get_event_loop()
) - such asasyncio.Lock
, does not work without grabbing and specifying the loop usingor by setting it to be the current one
Otherwise you get errors looking like this:
Cause
Since version Daphne 2.4.1 the running loop created in
https://github.com/django/daphne/blob/master/daphne/server.py#L7 (
twisted_loop = asyncio.new_event_loop()
)is no longer fetchable with
asyncio.get_event_loop()
This change in 2.4.1 came as a surprise to us and to find all the places the loop has to be specified will be tough considering it is a very common practice to rely on
asyncio.get_event_loop()
. Many installed packages rely on that. Also making sure tests handle it is difficult. Meaning we have to rely on solution 2 (asyncio.set_event_loop(twisted_loop)
).Solution
Either revert this change:
27f760a
or add
asyncio.set_event_loop(twisted_loop)
after
twisted_loop = asyncio.new_event_loop()
.Feedback
Switching from using the default event loop to custom one should probably not be considered to be a minor change.
The text was updated successfully, but these errors were encountered: