2

I'm using several methods of regex module. I need to set timeouts for multiple compiled patterns, but despite example from docs, I'm unable to reproduce an exception doing the following:

>>> import regex
>>> from time import sleep
>>> def slow_replace(m):
...     sleep(5)
...     return 'X'
...
>>> regex.sub(r'[a-z]', slow_replace, 'abcde', timeout=2)

It does not raise TimeoutError as expected.

I'm using python 3.8 on Ubuntu 20.04 LTS within Microsoft WSL2. I also found this related issue, but it did not help. How can I work around that issue?

2
  • 1
    Ubuntu probably works the same as MacOS, so I suspect it's that related issue. Show how you tried to implement the workaround. Commented Aug 12, 2021 at 16:18
  • I couldn't find any yet Commented Aug 13, 2021 at 16:56

1 Answer 1

-1

On mac os

import regex
import time
from time import sleep
def slow_replace(m):
    start = time.time()
    i = 0 
    while time.time()-start < 10:
        i += 1
    return 'X'

regex.sub(r'[a-z]', slow_replace, 'abcde', timeout=2)

raises a TimeoutError as expected.

Sign up to request clarification or add additional context in comments.

3 Comments

I couldn't reproduce
The problem here is not how to make it raise a TimeoutError given a sleep strategy, but how to make it break when a problematic regex is processed. It looks like search and finditer methods never break on timeout
@Kfcaio So you're actually asking why doesn't your slow_replace not raise a TimeoutError? As it is said in your link and as my example shows it's because the unrealistic behaviour of sleeping does not count as cpu time and cpu time is what produces a timeout. And I agree with people in your link in doubting that that's practically relevant. So I guess your need a better minimal reproducible example of what your actual problem is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.