Threading and multiprocessing eye-candy.
Reason this release was yanked:
Broken tag claiming Python 2 compatibility
Project description
Pebble provides a neat API to manage threads and processes within an application.
- Source:
- Documentation:
- Download:
Examples
Run a job in a separate thread and wait for its results.
from pebble import concurrent
@concurrent.thread
def function(foo, bar=0):
return foo + bar
future = function(1, bar=2)
result = future.result() # blocks until results are ready
Same code with AsyncIO support.
import asyncio
from pebble import asynchronous
@asynchronous.thread
def function(foo, bar=0):
return foo + bar
async def asynchronous_function():
result = await function(1, bar=2) # blocks until results are ready
print(result)
asyncio.run(asynchronous_function())
Run a function with a timeout of ten seconds and deal with errors.
from pebble import concurrent
from concurrent.futures import TimeoutError
@concurrent.process(timeout=10)
def function(foo, bar=0):
return foo + bar
future = function(1, bar=2)
try:
result = future.result() # blocks until results are ready
except TimeoutError as error:
print("Function took longer than %d seconds" % error.args[1])
except Exception as error:
print("Function raised %s" % error)
print(error.traceback) # traceback of the function
Pools support workers restart, timeout for long running tasks and more.
from pebble import ProcessPool
from concurrent.futures import TimeoutError
TIMEOUT_SECONDS = 3
def function(foo, bar=0):
return foo + bar
def task_done(future):
try:
result = future.result() # blocks until results are ready
except TimeoutError as error:
print("Function took longer than %d seconds" % error.args[1])
except Exception as error:
print("Function raised %s" % error)
print(error.traceback) # traceback of the function
with ProcessPool(max_workers=5, max_tasks=10) as pool:
for index in range(0, 10):
future = pool.submit(function, TIMEOUT_SECONDS, index, bar=1)
future.add_done_callback(task_done)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Pebble-5.0.1.tar.gz
(29.0 kB
view hashes)
Built Distribution
Pebble-5.0.1-py2.py3-none-any.whl
(30.0 kB
view hashes)
Close
Hashes for Pebble-5.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ae712b40211fa731309dd6e846efd98da831ae546e300ff6f03a351621505c4 |
|
MD5 | 8c6c3434c736c52fb92ea4a6783defe0 |
|
BLAKE2b-256 | 3b761cd1f6527763744d5350477715267a27ad92b22bc26c6d8db980a015df68 |