File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22# -*- coding: utf-8 -*-
33
44import os
5+ import errno
56import shelve
7+ import signal
8+ from functools import wraps
69from typing import NamedTuple , List , Any
710
811from . import constants
1316import symbols
1417
1518
16- __all__ = ['read_txt_file' , 'open_file' , 'sanitize_filename' , 'flatten_list' ]
19+ __all__ = [
20+ 'read_txt_file' ,
21+ 'open_file' ,
22+ 'sanitize_filename' ,
23+ 'flatten_list' ,
24+ 'timeout'
25+ ]
1726
1827__doc__ = """Utils module contains many helpers for several task, like reading files
1928or path management"""
@@ -136,3 +145,22 @@ def get_final_value(symbol: symbols.SYMBOL):
136145 result = result .value
137146
138147 return result
148+
149+
150+ def timeout (seconds = 10 , error_message = os .strerror (errno .ETIME )):
151+ def decorator (func ):
152+ def _handle_timeout (signum , frame ):
153+ raise TimeoutError (error_message )
154+
155+ def wrapper (* args , ** kwargs ):
156+ signal .signal (signal .SIGALRM , _handle_timeout )
157+ signal .alarm (seconds )
158+ try :
159+ result = func (* args , ** kwargs )
160+ finally :
161+ signal .alarm (0 )
162+ return result
163+
164+ return wraps (func )(wrapper )
165+
166+ return decorator
You can’t perform that action at this time.
0 commit comments