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.1.1.tar.gz
(38.7 kB
view details)
Built Distribution
pebble-5.1.1-py3-none-any.whl
(34.6 kB
view details)
File details
Details for the file pebble-5.1.1.tar.gz
.
File metadata
- Download URL: pebble-5.1.1.tar.gz
- Upload date:
- Size: 38.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
4e91a5b8e48b30b26eaa5391ba2cf65fbb3594fba17b88bc0b3351cf849d0305
|
|
MD5 |
4160e1e7c6c5480bc144d70ea4d837e6
|
|
BLAKE2b-256 |
5f5b90bf2acce03a12750a570cede27b9cddd4b6f5f2cf4de1048231bb21c382
|
File details
Details for the file pebble-5.1.1-py3-none-any.whl
.
File metadata
- Download URL: pebble-5.1.1-py3-none-any.whl
- Upload date:
- Size: 34.6 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 |
c262c94159cf6419fd4ac27ff72408650a16d6c3cf000171fb2a0386038c416e
|
|
MD5 |
235e64fb87ad686647fceca0b7b5c7ce
|
|
BLAKE2b-256 |
4da7bece0e6b17b75506c4bf13eb79728931e2f240d7928a02f0e6d2ca9c41e9
|