Skip to main content

Threading and multiprocessing eye-candy.

Project description

Pebble provides a neat API to manage threads and processes within an application.

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

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

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 i in range(0, 10):
        future = pool.schedule(function, args=[i], timeout=3)
        future.add_done_callback(task_done)

More examples in the documentation.

Project details


Release history Release notifications | RSS feed

This version

4.3.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Pebble-4.3.1.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Pebble-4.3.1-py2.py3-none-any.whl (21.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file Pebble-4.3.1.tar.gz.

File metadata

  • Download URL: Pebble-4.3.1.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Pebble-4.3.1.tar.gz
Algorithm Hash digest
SHA256 64b496da29add986b211ed53110b1dc5df34b7f84e4e65fc10ac266076c92870
MD5 ad824418039523e83af99a5281d9bb2f
BLAKE2b-256 6efa1d7ac2433af3df7f7e0c158909087b2242e3e66b7476f34dfc9a8d253fe3

See more details on using hashes here.

File details

Details for the file Pebble-4.3.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for Pebble-4.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3b2eeb58ccbf2bb2c95cbc57a4bd0d6206677ba790e870e9d95b00ef6f00d802
MD5 898a9e06fa0dde0ccde4c89a7c4287b7
BLAKE2b-256 fcef2a369d785c7b5f6766f9adfc39da7e4e35fd158ec068c895780aec8adcfe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page