반응형
ex)
import signal
import time
def log_print() :
count = 1
while 1:
print ("count : ", count)
count = count + 1
time.sleep(1)
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)
# timeout code seconds setting
try :
with timeout(seconds=10):
log_print()
except TimeoutError as e :
print ("Exception message : ", e)
윈도우에서 실행하면 AttributeError: module 'signal' has no attribute 'SIGALRM' 라는 에러가 발생.
linux 환경에서 사용한 코드
반응형
'프로그램 > Python' 카테고리의 다른 글
pytube exception : RegexMatchError (0) | 2019.04.29 |
---|---|
error : UnicodeEncodeError: 'cp949' codec can't encode character '\u2764' in position 19: illegal multibyte sequence (0) | 2018.11.26 |
python - OptionParser (3) | 2013.05.21 |
python – os.glob 모듈 (0) | 2013.05.21 |
python split() (0) | 2013.05.21 |