Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Add support for PEP 567 context variables to tasklets #239

Closed
akruis opened this issue Apr 18, 2021 · 4 comments
Closed

Add support for PEP 567 context variables to tasklets #239

akruis opened this issue Apr 18, 2021 · 4 comments

Comments

@akruis
Copy link

akruis commented Apr 18, 2021

Python 3.7 added the context variables and the module contextvars. See PEP 567 for details.

Using tasklets it is possible to decorate a plain old callable as a coroutine (and vice versa). To make such a coroutine fully compatible with C-Python asyncio tasklets should support PEP 567 context variables. In C-Python the current context is a property of the thread state, that is, each thread has its own current context. In Stackless Python a thread can have multiple tasklets. Therefore the current context must become a property of the tasklet.

I'm still elaborating details.

@kristjanvalur
Copy link
Collaborator

kristjanvalur commented Apr 19, 2021 via email

@akruis
Copy link
Author

akruis commented Apr 20, 2021

First usable code is in https://github.com/akruis/cpython/tree/slp37 (a71d969f06). It is still work in progress.
And here is the project, that needs the contextvar support https://github.com/akruis/slp_coroutine. This project is also work in progress.

akruis pushed a commit that referenced this issue May 18, 2021
Use a Py_LOCAL_INLINE function instead of a complicated macro. It is
much simpler to debug. In the next commit, we will context switching
to this function.
akruis added a commit that referenced this issue May 18, 2021
Add a private context attribute and appropriate methods to class
tasklet. Document the changes in the manual.
New methods: tasklet.set_context(context), tasklet.context_run(...)
New readonly attribute: tasklet.context_id
Add/improve pickling of the context of tasklets.
New pickle flag "PICKLEFLAGS_PICKLE_CONTEXT", new undocumented
function stackless._tasklet_get_unpicklable_state()
@akruis
Copy link
Author

akruis commented May 18, 2021

Implemented in 3.7-slp

akruis pushed a commit that referenced this issue Jun 5, 2021
Use a Py_LOCAL_INLINE function instead of a complicated macro. It is
much simpler to debug. In the next commit, we will context switching
to this function.

(cherry picked from commit a11414b)
akruis added a commit that referenced this issue Jun 5, 2021
Add a private context attribute and appropriate methods to class
tasklet. Document the changes in the manual.
New methods: tasklet.set_context(context), tasklet.context_run(...)
New readonly attribute: tasklet.context_id
Add/improve pickling of the context of tasklets.
New pickle flag "PICKLEFLAGS_PICKLE_CONTEXT", new undocumented
function stackless._tasklet_get_unpicklable_state()

(cherry picked from commit 032a566)
@akruis
Copy link
Author

akruis commented Jun 5, 2021

Merged into main-slp.

@akruis akruis closed this as completed Jun 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants