Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PoliteInfiniteComputer created by acwoss - https://repl.it/@acwoss/PoliteInfiniteComputer
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
You can’t perform that action at this time.