Skip to main content

Threading and multiprocessing eye-candy.

Project description

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

Source:

https://github.com/noxdafox/pebble

Documentation:

https://pebble.readthedocs.io

Download:

https://pypi.org/project/Pebble/

Build Status Documentation Status PyPI - Downloads

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.2.1.tar.gz (40.5 kB view details)

Uploaded Source

Built Distribution

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

pebble-5.2.1-py3-none-any.whl (35.0 kB view details)

Uploaded Python 3

File details

Details for the file pebble-5.2.1.tar.gz.

File metadata

  • Download URL: pebble-5.2.1.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pebble-5.2.1.tar.gz
Algorithm Hash digest
SHA256 9743537aa0e40751a1cbf297928e141bd2202a24624a7c0f9fc14818a7f227b1
MD5 9bc72d84a2f7b0cd861dd262afb15408
BLAKE2b-256 f96bfba8cb6ca12e6bbcfac48da790b6a9d262d5beb6f34dc1b63fccc32a9ff8

See more details on using hashes here.

File details

Details for the file pebble-5.2.1-py3-none-any.whl.

File metadata

  • Download URL: pebble-5.2.1-py3-none-any.whl
  • Upload date:
  • Size: 35.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pebble-5.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 154feb48f187ffff842cbc2fe0768ed1524d6fa72e90577a78bcea21a2f929f9
MD5 bd9ae0acbfba70961de2cb2fe3c1cc62
BLAKE2b-256 adcff838e54b229bb202575daf7542d39da3c38434959d7a9252d4be66cb7d9a

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