Strange problem when using re module with threads
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Nov 2 01:44:46 EDT 2007
En Thu, 01 Nov 2007 17:01:36 -0300, Wei Lee Woon <wlwoon at gmail.com>
escribió:
> I've been getting a rather strange problem with the following
> multithreaded
> 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();
>
> thlist=[]
> for tech in range(2):
> thlist.append(hey())
> thlist[-1].start()
>
> for th in thlist:
> th.join()
>
> After saving this to a file (say "test.py"), if I try to run this from
> the
> console using "python test.py", it seems to work fine, but when i try to
> run
> it from the python interactive shell using "import test", it freezes up
> (if
> 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.
--
Gabriel Genellina
More information about the Python-list
mailing list