Created
May 10, 2018 03:18
-
-
Save acwoss/3b74275be3d4063251796c615385cba5 to your computer and use it in GitHub Desktop.
PoliteInfiniteComputer created by acwoss - https://repl.it/@acwoss/PoliteInfiniteComputer
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
from multiprocessing import TimeoutError | |
from multiprocessing.pool import ThreadPool | |
def timeout(seconds): | |
def decorator(function): | |
def wrapper(*args, **kwargs): | |
pool = ThreadPool(processes=1) | |
result = pool.apply_async(function, args=args, kwds=kwargs) | |
try: | |
return result.get(timeout=seconds) | |
except TimeoutError as e: | |
return e | |
return wrapper | |
return decorator | |
@timeout(5) | |
def read_user_name(): | |
return input('Nome? ') | |
name = read_user_name() | |
if isinstance(name, TimeoutError): | |
print('Demorou demais, parsa!') | |
else: | |
print('Olá', name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment