Last active
December 2, 2023 23:13
-
-
Save milimetric/2a9ee4eaad6d2a1523dece6166e2eb90 to your computer and use it in GitHub Desktop.
Set a timeout for executing python code in a with statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import signal | |
import re | |
class TimeoutError(Exception): | |
pass | |
class timeout: | |
def __init__(self, seconds=1, error_message='Timeout'): | |
self.seconds = seconds | |
self.error_message = error_message | |
def handle_timeout(self, signum, frame): | |
raise TimeoutError(self.error_message) | |
def __enter__(self): | |
signal.signal(signal.SIGALRM, self.handle_timeout) | |
signal.alarm(self.seconds) | |
def __exit__(self, type, value, traceback): | |
signal.alarm(0) | |
ua = 'Mozilla/5.0 (X11; Linux x86_64_128) AppleWebKit/11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.111111111111111111111111111111111111111111111111111111111111111111111111 (KHTML, like Gecko) Linux/222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222' | |
with timeout(3): | |
r = re.compile('^(.*)/(\d+)\.?(\d+)?.?(\d+)?.?(\d+)? CFNetwork') | |
print(r.match(ua[:10]) or 'No match with a 10-long substring') | |
with timeout(3): | |
r = re.compile('^(.*)/(\d+)\.?(\d+)?.?(\d+)?.?(\d+)? CFNetwork') | |
print(r.match(ua)) | |
# import time | |
# import random | |
# ************** timeout test | |
# with timeout(seconds=3): | |
# time.sleep(4) | |
# ************** exception test | |
# with timeout(seconds=3): | |
# time.sleep(2) | |
# raise Exception('a different thing') | |
# ************** overhead test | |
# count = 100000 | |
# x = '' | |
# start = time.time() | |
# for i in range(count): | |
# x = x + str(random.randint(0, count)) | |
# stop = time.time() | |
# print('without timeout ' + str(stop - start)) | |
# x = '' | |
# start = time.time() | |
# with timeout(seconds=5): | |
# for i in range(count): | |
# x = x + str(random.randint(0, count)) | |
# stop = time.time() | |
# print('with timeout ' + str(stop - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment