Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PoliteInfiniteComputer created by acwoss -
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)
return result.get(timeout=seconds)
except TimeoutError as e:
return e
return wrapper
return decorator
def read_user_name():
return input('Nome? ')
name = read_user_name()
if isinstance(name, TimeoutError):
print('Demorou demais, parsa!')
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.