Cancel or timeout a long running regular expression

Terry Reedy tjreedy at
Thu Sep 15 14:54:57 EDT 2011

On 9/15/2011 1:19 AM, python at wrote:
> Is there a way to cancel or timeout a long running regular expression?
> I have a program that accepts regular expressions from users and I'm
> concerned about how to handle worst case regular expressions that seem
> to run forever. Ideally I'm looking for a way to evaluate a regular
> expression and timeout after a specified time period if the regular
> expression hasn't completed yet. Or a way for a user to cancel a long
> running regular expression.

This is a general problem when evaluating *any* expression from the 
outside. [0]*10000*10000 will eat space as well as time. At least, as 
far as I know, an re cannot cause a disk reformat ;-).

There have been previous discussions on this generally topic.

> I was thinking there might be a technique I could use to evaluate
> regular expressions in a thread or another process launched via
> multiprocessing module and then kill the thread/process after a
> specified timeout period.

Only solution I remember ever seen posted. I wonder if there are any 
heuristics for detecting exponential time re's.

 > My concern about the multiprocessing module
> technique is that launching a process for every regex evaluation sounds
> pretty inefficient. And I don't think the threading module supports the
> ability to kill threads from outside a thread itself.

Terry Jan Reedy

More information about the Python-list mailing list