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

[RFC] Asyncio Support #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

[RFC] Asyncio Support #11

wants to merge 2 commits into from

Conversation

fantix
Copy link
Member

@fantix fantix commented Aug 29, 2018

This is a proposal to fix #2 in Approach 2, with patching code to achieve context copying on task creation, please see separate commits in this PR. Regarding context copying/inheriting:

  • After importing contextvars with this PR, all create_task() calls of event loops which went through either asyncio.get_event_loop(), asyncio.new_event_loop() or asyncio.set_event_loop() would implicitly do an extra context copy, and use it as task context.
  • Current PR requires less user code. User just needs to import contextvars before initializing event loop policies or creating event loops, and all tasks created by automatically patched loops could inherit the context on creation.
  • But get_event_loop_policy() shall no longer return the policy passed to set_event_loop_policy() with this PR - a wrapper policy is returned. Although the wrapper works in the same way as the original policy, get_event_loop_policy().policy should be used if original policy instance is required.

Other than patching, it is also possible to use get_task_factory() and set_task_factory() to achieve the same result, as demonstrated here. Pros and cons:

  • User needs to explicitly call enable_inherit(loop) for any loop in use.
  • But forgetting to do so will end up with tasks behaving differently.
  • However this approach doesn't patch anything, so APIs are untouched.

At last, the reason for using Approach 2 of #2 is:

  1. I didn't want Approach 1 to copy lots of asyncio code from Python 3.7 into this package. But I'm still open to that, please advise.
  2. Tried an Approach 3 but failed. (Created a wrapper event loop providing the new APIs, but the loop relationship is a mess - the underlying loop creates a Future whose _loop is underlying loop, but when using the wrapper loop to e.g. gather() it checks future._loop is loop and fails. Or vise versa. Also Task code needs to be backported anyway.)
  3. Couldn't find a new Approach 4.

UPDATE: This is now under testing in practice as aiocontextvars 0.2 in GINO and possibly some other projects.

@whardier
Copy link

Checking in on this. Would be useful to use this in an large open source project supporting AWS runtimes.

@fantix
Copy link
Member Author

fantix commented Apr 29, 2022

Checking in on this. Would be useful to use this in an large open source project supporting AWS runtimes.

Any luck to upgrade to use Python 3.7? I think as Python 3.6 security support ended 4 months ago, this PR should probably be closed.

@whardier
Copy link

Wouldn't that be awesome? Python 3.6.2+ still in use in our user base.. thanks.

If anything we can vendor this in as needed. Thanks!

@fantix
Copy link
Member Author

fantix commented Apr 29, 2022

Ouch, sorry to hear that. Unfortunately I don't think this PR will go any further, projects on 3.6 would have to hammer in similar hacks by themselves I'm afraid.

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

Successfully merging this pull request may close these issues.

asyncio support
2 participants