pombreda / gevent

Automatically exported from code.google.com/p/gevent
0 stars 0 forks source link

monkey.patch_thread() breaks threading.local #24

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
run threadlocaltest.py to see that the thread doesn't have its own local 
data:

$ python2.6 local.py
func's view of localdata.__dict__: {'x': "hello, hope you can't see this"}

Comment out monkey.patch_thread() to see the correct behavior:
$ python2.6 local.py
func's view of localdata.__dict__: {}

gevent 0.12.2 + python 2.6 on centos

Original issue reported on code.google.com by tsuz...@gmail.com on 30 Apr 2010 at 6:20

Attachments:

GoogleCodeExporter commented 9 years ago
this example (which uses the same patch technique as monkey.patch_thread()) 
shows the 
underlying problem on my python 2.6.4 install:

---
import thread
import _threading_local

thread._local = _threading_local.local

import threading

print "threading.local is", threading.local, "instead of", 
_threading_local.local
---

I get:

---
threading.local is <type 'thread._local'> instead of <class 
'_threading_local.local'>
---

I added a "print thread._local" line to the top of my install's threading.py, 
and it 
prints out <type 'thread._local'>, despite the line directly above 'import 
threading' 
that sets thread._local to _threading_local.local. Somehow, threading.py in the 
python install is magically finding the original <type 'thread._local'>.

I copied my python's threading.py to mythreading.py and put it in the same 
directory 
as my python test script. If I import mythreading instead of threading, the 
problem 
goes away. I don't know enough about python's import system to debug any 
further. 
Hopefully this problem is only affecting my install. I'm working around it by 
adding 
"threading.local = _threading_local.local" after I import threading.

Original comment by tsuz...@gmail.com on 30 Apr 2010 at 7:49

GoogleCodeExporter commented 9 years ago
Verified that this problem can be simply fixed by adding the following lines to 
the beginning of your program.

---
import gevent.monkey
gevent.monkey.patch_all()

import threading
import thread
threading.local = thread._local
---

Original comment by thewrong...@gmail.com on 4 May 2010 at 3:53

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Please note that _threading_local is broken in many Python versions. You may 
want to 
apply the patch in the following issue first.

http://bugs.python.org/issue1522237

Original comment by thewrong...@gmail.com on 4 May 2010 at 10:03

GoogleCodeExporter commented 9 years ago
Fixed in the trunk. 

gevent now has gevent.local module:
http://bitbucket.org/denis/gevent/src/tip/gevent/local.py

gevent.monkey uses it when patching thread and it also patches threading now.

Original comment by Denis.Bi...@gmail.com on 20 May 2010 at 9:35

GoogleCodeExporter commented 9 years ago
Migrated to http://github.com/SiteSupport/gevent/issues/24

Original comment by Denis.Bi...@gmail.com on 14 Sep 2012 at 10:51