Regarding Regex timeout behavior to minimize CPU consumption

Shahique Khan shahiquzzama at gmail.com
Fri Nov 27 07:33:28 EST 2020


Hi Team,

I have noticed if our regex sometimes does not give a result and on that
time regex took more time in returning response (empty response).

My Question is can we set a timeout parameter (in seconds/millisecond) with
re.find or anywhere in code to avoid CPU consumption if regex takes more
time in execution.

Below is the example, which take more time in execution: (in this case can
we set timeout to kill the execution to avoid CPU consumption)

regex = r'data-stid="section-room-list"[\s\S]*?>\s*([\s\S]*?)\s*' \
        r'(?:class\s*=\s*"\s*sticky-book-now\s*"|</ul>\s*</section>|id\s*=\s*"Location")'
rooms_blocks_to_be_replace = re.findall(regex, html_template)


Please help me, I will be very thankful for this.

Thanks,


More information about the Python-list mailing list