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
Is eventlet compatible with ContextVars? #663
Comments
@pgjones I've put up a (probably wrong) test real quick and it seems that eventlet also should patch contextvars. # check.py
import eventlet
import random
import contextvars
ctx = contextvars.ContextVar('foo')
ctx.set(0)
def w(id):
for _ in range(10):
eventlet.sleep(random.random()/10)
print(f"{id}: set")
ctx.set(id)
eventlet.sleep(random.random()/10)
print(f"{id}: get", ctx.get())
t1 = eventlet.spawn(w, 1)
t2 = eventlet.spawn(w, 2)
t3 = eventlet.spawn(w, 3)
t1.wait()
t2.wait()
t3.wait()
Thanks for educating on this new library. |
@temoto thank you :). |
Some useful context here: greenlet added support for contextvars in 0.4.17 released in September. This is a nicer solution than adding a monkey patch, and is more in line with the intended use of contextvars as I understand it. I modified @temoto's script to assert on failure and sleep for 0 seconds to simply yield to another greenlet. This lets me run it for far more iterations; see below. I tested with a pretty old eventlet, 0.21, as well as the current version, In both cases, with greenlet 0.4.16, the script fails almost immediately. With 0.4.17 or 1.0.0 it succeeds. I think this issue can be closed with a clarification somewhere that greenlet 0.4.17 or 1.0.0 (just released yesterday) is used. My modified script for easier testing: # check.py
import eventlet
import contextvars
ctx = contextvars.ContextVar('foo')
ctx.set(0)
def w(id):
for _ in range(10000):
eventlet.sleep() # yield
ctx.set(id)
eventlet.sleep() # yield
assert ctx.get() == id, "FAILED %d != %d" % (id, ctx.get())
t1 = eventlet.spawn(w, 1)
t2 = eventlet.spawn(w, 2)
t3 = eventlet.spawn(w, 3)
t1.wait()
t2.wait()
t3.wait() |
If I'm understanding this right, it means that even though |
As gentleman above says, you just have to install recent greenlet and no
patching is needed.
…On Tue, Feb 9, 2021, 23:54 David Lord ***@***.***> wrote:
If I'm understanding this right, it means that even though
eventlet.patcher.is_monkey_patched("contextvars") still returns False,
they'll work correctly?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#663 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAGTMJTPRQLXVZFFI34S53S6GOHFANCNFSM4TEBJLKQ>
.
|
What's the status of this? It's still open, but the last comment seems to indicate that eventlet works with contextvars despite reporting them as not patched. We are dropping Python 3.6 support in Werkzeug, are moving from thread locals to contextvars, and would like to drop our compat code for eventlet if possible. |
status
|
PyPy comes with greenlet included, and it's not possible to This is an issue because Flask has been adding support for I understand Eventlet is relying on greenlet to support contextvars, but it seems important to have this working on PyPy one way or another. Perhaps if you added your voice to https://foss.heptapod.net/pypy/pypy/-/issues/3408 it may be addressed. |
Sorry for the confusion. Looks like PyPy 7.3.7, released last week, does have support for greenlet with contextvars, even though it still reports an old version number. |
Looks like gevent had to add a greenlet compatible implementation of ContextVars, issue and fix. Does eventlet need to do the same?
The text was updated successfully, but these errors were encountered: