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?

Kfcaio
  • 442
  • 1
  • 8
  • 20

1 Answers1

-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.

Lukas S
  • 3,212
  • 2
  • 13
  • 25
  • I couldn't reproduce – Kfcaio Aug 13 '21 at 16:50
  • 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 Aug 13 '21 at 16:59
  • @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. – Lukas S Aug 14 '21 at 00:03