hello don, On Tue, Feb 18, 2014 at 10:44:28AM +0100, chrysn wrote: > [... much ...]
had i read the python-tulip mailing list more carefully, i would have seen the "ThreadLocal analogue" thread. the whole hackery described in the abovementioned mail can be easily avoided by using `asyncio.Task.current_task()`. the attached new patch is much smaller, and i'd like to propose it for inclusion in your flask branch to solve the "Popped wrong request context" problem. compared to my last approach, it might cause troubles when users create a Task manually -- but then again, that only happens in asyncio aware applications, and they can pass on the request object explicitly again. i'll keep playing with your flask version, it feels pretty usable. (for the time being, even with sqlalchemy). afaict, your patch to flask is very small (just some additional yield-from, and some call_maybe_yield), and not even the werkzeug patch is too heavy. have you tried getting an opinion from the flask/werkzeug developers on inclusion yet, or an idea on how a version that can do both (classical and asyncio) can be maintained? best regards chrysn -- You don't use science to show that you're right, you use science to become right. -- Randall Munroe
diff --git a/werkzeug/local.py b/werkzeug/local.py
index 47a9264..bf33c62 100644
--- a/werkzeug/local.py
+++ b/werkzeug/local.py
@@ -12,17 +12,9 @@ from functools import update_wrapper
from werkzeug.wsgi import ClosingIterator
from werkzeug._compat import PY2, implements_bool
-# since each thread has its own greenlet we can just use those as identifiers
-# for the context. If greenlets are not available we fall back to the
-# current thread ident depending on where it is.
-try:
- from greenlet import getcurrent as get_ident
-except ImportError:
- try:
- from thread import get_ident
- except ImportError:
- from _thread import get_ident
-
+import asyncio
+def get_ident():
+ return id(asyncio.Task.current_task())
def release_local(local):
"""Releases the contents of the local for the current context.
signature.asc
Description: Digital signature
