Confirm: compiled re(gexps) are thread safe?

Discussion in 'Python' started by Johan Ovlinger, Mar 22, 2005.

  1. 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
    Johan Ovlinger, Mar 22, 2005
    #1
    1. Advertising

  2. Johan> Subject says it all, really.

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

    Skip
    Skip Montanaro, Mar 22, 2005
    #2
    1. Advertising

  3. 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>
    Fredrik Lundh, Mar 22, 2005
    #3
  4. Skip Montanaro wrote:
    > Johan> Subject says it all, really.
    >
    > Yes, searching using a compiled regular expression is thread-safe.
    >
    > Skip


    Great.

    Thanks
    Johan Ovlinger, Mar 22, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Klaus Schneider
    Replies:
    1
    Views:
    525
    Rolf Magnus
    Dec 2, 2004
  2. lander
    Replies:
    5
    Views:
    575
    bruce barker
    Mar 5, 2008
  3. jpatrcik
    Replies:
    3
    Views:
    1,152
    bruce barker
    May 23, 2008
  4. Gabriel Rossetti
    Replies:
    0
    Views:
    1,294
    Gabriel Rossetti
    Aug 29, 2008
  5. John Nagle
    Replies:
    5
    Views:
    450
    John Nagle
    Mar 12, 2012
Loading...

Share This Page