Confirm: compiled re(gexps) are thread safe?

Fredrik Lundh fredrik at pythonware.com
Tue Mar 22 20:00:19 CET 2005


Johan Ovlinger wrote:

> Can someone please confirm that I can safely do something like the below,
> without needing a thread-local compiled regexp?

yes.

> global_var = re.compile( "foo" )
>
> for str in manystrings:
>  def domatch(str):
>    if global_var.search(str):
>      print "yahooo!"
>  threading.thread(target = domatch, args = [str]).start()
>
> Alternately, what is the overhead in compilation? Neglible?

the compiler uses a cache, so even if you call compile inside each thread,
all threads end up getting the same pattern object.

on the other hand, make sure you google for "global interpreter lock" before
you spend too much time implementing parallell searches...

</F> 






More information about the Python-list mailing list