Repository navigation
asyncio with two interpreter instances #91375
Description
Activity
Hi,
I have an issue when using asyncio and two interpreter instances each launched and used in a seperated thread.
I am getting a asyncio loop for each thread .However asyncio is getting me the same loop because of this code in get_running_loop. Indeed when I have two interpreter, the ts_id would be the same for both my threads and therefore I will get the cached value of the first thread. cached_running_holder being static, it is the same value for all instances of interpreter.
Maybe we should check if we are in the same interpreter or same thread ,.. I am not sure how it could be fixed._asynciomodule.c: get_running_loop(PyObject **loop) { PyObject *rl; PyThreadState *ts = _PyThreadState_GET(); uint64_t ts_id = PyThreadState_GetID(ts); if (ts_id == cached_running_holder_tsid && cached_running_holder != NULL) {
If it does not make sense, I have some sample code but it is not just 10 lines.
Reacted by Pipiche and ryssonReacted by Pipiche- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Apr 4, 2022 hello, is there anything we can do to get some attention ?
This is a blocking issue in our project which spawn several instances from a Python embedded frameworkThanks in advance
It looks like asyncio has not been updated to support use in multiple interpreters -- the module uses a whole bunch of global variables to store state. Until that is dealt with, asyncio cannot be used reliably with multiple interpreters. See PEP 687. @erlend-aasland
Also note that this is unlikely to be considered a bug, so no backports. Likewise it is unlikely to happen for 3.11 since the feature freeze is literally today. Thus the soonest you may see a fix is 3.12 (Fall 2023), unfortunately.
Reacted by PipicheReacted by PipicheYeah, as soon as PEP 687 land, we can start the process of implementing this. I believe I've got a WIP branch lying around already.
Reacted by Eric SnowThanks for the update, we will be patient. just a shame not to have back port, but understood the amount of work.
Now, we would be really please to test it when you want .Potential testing platforms OS: raspian or fedora
Reacted by Eric SnowI can push it to my fork next week, so you can play with it.
One workaround currently is to not use
_asynciospeed up module but to use pure python version as that does not has this limitation.Reacted by Erlend E. AaslandOne workaround currently is to not use
_asynciospeed up module but to use pure python version as that does not has this limitation.interesting. How to you prevent using _asyncio ?
Before importing
asyncioadd this line:import sys sys.modules["_asyncio"] = None
Reacted by Erlend E. Aasland, Sylvain PERON and rysson@kumaraditya303 if ayncio is imported in many module. does it enough to do it only once while importing asyncio for the first time ?
[edit] at least I did it prior the first occurence of import and this looks pretty good .
24 remaining items
Thanks for working on this, @kumaraditya303!
Reacted by Kumar Aditya and Erlend E. AaslandReopening: the future iter object freelist is still a static global, and we forgot to clean up
Tools/c-analyzer/cpython/globals-to-fix.tsv.Reopening: the future iter object freelist is still a static global, and we forgot to clean up Tools/c-analyzer/cpython/globals-to-fix.tsv.
I left that freelist intentionally as it is not an issue until we have per interpreter GIL. @markshannon has ideas for a global better freelist so hopefully we should be able to remove this freelist entirely.
Reacted by Erlend E. AaslandI left that freelist intentionally as it is not an issue until we have per interpreter GIL. @markshannon has ideas for a global better freelist so hopefully we should be able to remove this freelist entirely.
I see, thanks for the heads-up.
- added a commit that references this issue
on Jan 24, 2023 I left that freelist intentionally as it is not an issue until we have per interpreter GIL. @markshannon has ideas for a global better freelist so hopefully we should be able to remove this freelist entirely.
Hm. I wouldn't necessarily count on Mark's single freelist idea to be implemented before we get a per-subinterpreter GIL (esp. since the work on mimalloc seems stalled, alas -- Christian Heimes seems to be occupied by other things).
So I think it would behoove us to move
fi_freelistandfi_freelist_lento the "per runtime" structure? Or what am I missing?
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
_asynciostatic types to heap types and module state #99122