- Notifications
You must be signed in to change notification settings - Fork55
Multi threading and processing eye-candy.
License
NotificationsYou must be signed in to change notification settings
noxdafox/pebble
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Pebble provides a neat API to manage threads and processes within an application.
Source: | https://github.com/noxdafox/pebble |
---|---|
Documentation: | https://pebble.readthedocs.io |
Download: | https://pypi.org/project/Pebble/ |
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)
About
Multi threading and processing eye-candy.
Topics
Resources
License
Code of conduct
Stars
Watchers
Forks
Packages0
No packages published