Confirm: compiled re(gexps) are thread safe?

J

Johan Ovlinger

Subject says it all, really.

Can someone please confirm that I can safely do something like the below,
without needing a thread-local compiled regexp? (not run, so probably rife
with syntax errors, but you get the drift)

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?

Thanks,

Johan
 
S

Skip Montanaro

Johan> Subject says it all, really.

Yes, searching using a compiled regular expression is thread-safe.

Skip
 
F

Fredrik Lundh

Johan said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top