[issue10393] "with" statement isn't thread-safe

Amaury Forgeot d'Arc report at bugs.python.org
Fri Nov 12 15:39:32 CET 2010


Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

Agreed. There was a similar issue in python2.6 with list comprehensions.
The issue is that both threads run the code with the same globals dictionary, and top-level code use the same dict for locals and globals.

This is already fixed in 2.7 and 3.2, temporary variables are no more used.

Meanwhile, I can see two workarounds:
- Run the code in a python function; the _[1] variable will be local to the function, and distinct for each thread.
- Use PyRun_String(), and pass a different dictionary for each thread::

    PyObject *d, *v;
    d = PyDict_New();
    v = PyRun_String(command, Py_file_input, d, d);
    Py_XDECREF(v);
    Py_XDECREF(d);

But the 3.2 fix cannot be ported to 3.1: it needs a new opcode.

----------
nosy: +amaury.forgeotdarc
resolution:  -> out of date
status: open -> pending

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10393>
_______________________________________


More information about the Python-bugs-list mailing list