Message141548
When trying to cPickle.loads() from several threads at once, there is a race condition when threads try to import modules.
An example will explain it best:
suppose I have module foo.py which takes some time to load:
import time
class A(object):
def __setstate__(self, state):
self.x = x
time.sleep(1)
x = 5
and a pickled version of an A() object stored in 'A.pkl'.
the following code, when run for the first time, will raise a NameError about 'x':
>>> p = open('A.pkl','rb').read()
>>> [thread.start_new(cPickle.loads, (p,)) for x in xrange(2)]
Unhandled exception in thread started by <built-in function loads>
Traceback (most recent call last):
File "foo.py", line 7, in __setstate__
self.x = x
NameError: global name 'x' is not defined
since the module is now loaded, subsequent calls to cPickle.loads will work as expected.
This was tested on 2.5.2, 2.7.1, and 3.2 on Ubuntu and on Windows 7.
please note that this bug was discovered when unpickling the standard 'decimal.Decimal' class (decimal.py is quite long and takes some time to import), and this is not some corner case. |
|
Date |
User |
Action |
Args |
2011-08-02 08:16:09 | Sagiv.Malihi | set | recipients:
+ Sagiv.Malihi |
2011-08-02 08:16:09 | Sagiv.Malihi | set | messageid: <1312272969.55.0.673755441724.issue12680@psf.upfronthosting.co.za> |
2011-08-02 08:16:08 | Sagiv.Malihi | link | issue12680 messages |
2011-08-02 08:16:08 | Sagiv.Malihi | create | |
|