Threading and multiprocessing eye-candy.
Project description
Description
Pebble provides a neat API to manage threads and processes within an application.
Examples
Spawn a function within a thread:
from pebble import thread
def function(foo, bar=0):
print foo + bar
if __name__ == "__main__":
thrd = thread.spawn(target=function, args=[1], kwargs={'bar':2})
thrd.join()
Most of the functions work as well as decorators:
from pebble import process
@process.spawn(daemon=True)
def function(foo, bar=0):
print(foo + bar)
if __name__ == "__main__":
proc = function(1, bar=2)
proc.join()
Run a job in a separate process and wait for its results:
from pebble import process
@process.concurrent
def function(foo, bar=0):
return foo + bar
if __name__ == "__main__":
task = function(1, bar=2)
results = task.get() # blocks until results are ready
Pools allow to execute several tasks without the need of spawning a new worker for each one of them:
from threading import current_thread
from pebble import thread
def task_done(task):
results, thread_id = task.get()
print "Task %s returned %d from thread %s" % (task.id,
results,
thread_id)
def do_job(foo, bar=0):
return foo + bar, current_thread().ident
if __name__ == "__main__":
with thread.Pool(workers=5) as pool:
for i in range(0, 10):
pool.schedule(do_job, args=(i, ), callback=task_done)
Check the documentation for more examples.
TODO:
taskselect, queueselect, threadselect, procselect: wait for multiple Tasks, Queues, Threads and Processes
channels for message driven synchronization
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-3.1.1.tar.gz
(18.2 kB
view details)
File details
Details for the file Pebble-3.1.1.tar.gz.
File metadata
- Download URL: Pebble-3.1.1.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd1d8a0a2cbc6c16671a18d427b1aa4fd6aa047794134c4ff78fc1e7d78d2e44
|
|
| MD5 |
f95d91c88796358a2b2915a6ac0a94e1
|
|
| BLAKE2b-256 |
07602dd76d9ecb4229615ba56b4dd7e2d17501658b36452d307b20f22357f6bd
|