Strange problem when using re module with threads
gagsl-py2 at yahoo.com.ar
Fri Nov 2 06:44:46 CET 2007
En Thu, 01 Nov 2007 17:01:36 -0300, Wei Lee Woon <wlwoon at gmail.com>
> I've been getting a rather strange problem with the following
> code (reduced to the minimum which still results in the problem):
> import threading
> import re
> class hey(threading.Thread):
> def run(self):
> print re.compile("\d+").search("hey95you").group();
> for tech in range(2):
> for th in thlist:
> After saving this to a file (say "test.py"), if I try to run this from
> console using "python test.py", it seems to work fine, but when i try to
> it from the python interactive shell using "import test", it freezes up
> i don't issue the join() it is fine, though). Any ideas why this is so?
It's the "import lock" in action.
When an import is being made, no other thread can import anything, they
block waiting for the lock to be released. re.compile internally tries to
import sre_parse, and blocks. Then both threads are waiting for the import
lock, and the main thread is waiting in the join() call [inside the
"import test" call] - a deadlock.
Note that you get the same effect if you replace the re.compile(...) with
a simple "import os"
So, don't do blocking actions (like join()) at import time.
More information about the Python-list