By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Confirm: compiled re(gexps) are thread safe?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Johan> Subject says it all, really.

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

Skip
Jul 18 '05 #2

P: n/a
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>

Jul 18 '05 #3

P: n/a
Skip Montanaro wrote:
Johan> Subject says it all, really.

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

Skip


Great.

Thanks
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.