You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Run a job in a separate thread and wait for its results.
frompebbleimportconcurrent@concurrent.threaddeffunction(foo, bar=0):
returnfoo+barfuture=function(1, bar=2)
result=future.result() # blocks until results are ready
Same code with AsyncIO support.
importasynciofrompebbleimportasynchronous@asynchronous.threaddeffunction(foo, bar=0):
returnfoo+barasyncdefasynchronous_function():
result=awaitfunction(1, bar=2) # blocks until results are readyprint(result)
asyncio.run(asynchronous_function())
Run a function with a timeout of ten seconds and deal with errors.
frompebbleimportconcurrentfromconcurrent.futuresimportTimeoutError@concurrent.process(timeout=10)deffunction(foo, bar=0):
returnfoo+barfuture=function(1, bar=2)
try:
result=future.result() # blocks until results are readyexceptTimeoutErroraserror:
print("Function took longer than %d seconds"%error.args[1])
exceptExceptionaserror:
print("Function raised %s"%error)
print(error.traceback) # traceback of the function
Pools support workers restart, timeout for long running tasks and more.
frompebbleimportProcessPoolfromconcurrent.futuresimportTimeoutErrorTIMEOUT_SECONDS=3deffunction(foo, bar=0):
returnfoo+bardeftask_done(future):
try:
result=future.result() # blocks until results are readyexceptTimeoutErroraserror:
print("Function took longer than %d seconds"%error.args[1])
exceptExceptionaserror:
print("Function raised %s"%error)
print(error.traceback) # traceback of the functionwithProcessPool(max_workers=5, max_tasks=10) aspool:
forindexinrange(0, 10):
future=pool.schedule(function, index, bar=1, timeout=TIMEOUT_SECONDS)
future.add_done_callback(task_done)