Is re thread-safe?

B

Benjamin Han

Just want to make sure before I start to change my code in major way - it'd
be nice to do regex matching in parallel. Any one in the know please give
a hint. Thanks!
 
J

Jeff Epler

I don't know of anything about regular expression objects that would not
be thread safe. They don't have any data attributes that depend on the
current or last matching operation, for instance.

Internally, the C implementation of regular expressions is either
thread-safe or else runs with Python's GIL held to avoid thread-safety
issues.

It's possible that there's a particular concern I've overlooked--for
instance, REs can refer to character classes such as "alphanumeric"
(with \w, it looks like [:CLASS:]es are not supported). That means that
the meaning of a regular expression can depend on the state of the
program's locale, which is not a per-thread attribute.

Jeff
 
M

Michael Hudson

Benjamin Han said:
Just want to make sure before I start to change my code in major way - it'd
be nice to do regex matching in parallel.

Um. I doubt the sre engine releases the GIL during matching -- might
be wrong -- but unless you're on a multiprocessor I'm not quite sure
what "in parallel" would mean.

I don't think you'll encounter misbehaviour from using re in multiple
threads.

Cheers,
mwh
 

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
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top