Threading and multiprocessing eye-candy.
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.schedule(function, index, bar=1, timeout=TIMEOUT_SECONDS)
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.7.tar.gz
(32.1 kB
view details)
Built Distribution
Pebble-5.0.7-py3-none-any.whl
(31.0 kB
view details)
File details
Details for the file Pebble-5.0.7.tar.gz
.
File metadata
- Download URL: Pebble-5.0.7.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2784c147766f06388cea784084b14bec93fdbaa793830f1983155aa330a2a6e4 |
|
MD5 | 657ae3d58cc82124983048acd9ed9c89 |
|
BLAKE2b-256 | 349dc7a0c8cfb32f532c63a4ef816b8bcf03d6e6ef63617713fb0953cfe7052c |
File details
Details for the file Pebble-5.0.7-py3-none-any.whl
.
File metadata
- Download URL: Pebble-5.0.7-py3-none-any.whl
- Upload date:
- Size: 31.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1742f2a62e8544e722c7b387211fb1a06038ca8cda322e5d55c84c793fd8d7d |
|
MD5 | f72078299d381c37a73243d7d1dc90d6 |
|
BLAKE2b-256 | da31ac6157816f5ed9440f3ad8744dc171f51ec59cc6c0b50c70d3c8a09add2f |