Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Confirm: compiled re(gexps) are thread safe?

Reply
Thread Tools

Confirm: compiled re(gexps) are thread safe?

 
 
Johan Ovlinger
Guest
Posts: n/a
 
      03-22-2005
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
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      03-22-2005

Johan> Subject says it all, really.

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

Skip
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-22-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>



 
Reply With Quote
 
Johan Ovlinger
Guest
Posts: n/a
 
      03-22-2005
Skip Montanaro wrote:
> Johan> Subject says it all, really.
>
> Yes, searching using a compiled regular expression is thread-safe.
>
> Skip


Great.

Thanks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do compiled library dll's in ASP.NEt need to be thread safe? jpatrcik ASP .Net 3 05-23-2008 08:59 PM
If I create a page, then it's compiled upon first request, where cani find the compiled code?? lander ASP .Net 5 03-05-2008 04:34 PM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
g++ compiled C++ code called from gcc compiled C code Klaus Schneider C++ 1 12-02-2004 01:44 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM



Advertisments