Is re thread-safe?

Jeff Epler jepler at unpythonic.net
Wed Dec 24 21:38:41 EST 2003


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





More information about the Python-list mailing list