Skip to main content

Python PromiseKeeper

This module creates a multi-threaded system that you can submit work to, to be executed asynchronously. It will return immediately with a promise that you can periodically check to see if the work is done.

The main idea is to instantiate an instance of the PromiseKeeper and then submit requests for it to do work on your behalf. It will schedule your request to be executed on a thread and return a Promise instance to you. You can look at the promise to determine the status of the execution.

The promise keeper will track the results (return value) of your requested work and include it in the Promise that it returned to you when you submitted the request. If your work request raises an exception, the PromiseKeeper will capture the exception and include that in the Promise as well.

Here's a quick example:

#!/usr/bin/env python

from promise_keeper import PromiseKeeper
from random import random
from time import sleep

def slow_square(x: int) -> int:
    sleep(random() * 3)
    return x*x

pk = PromiseKeeper()

# doesn't block
promise = pk.submit(slow_square, (10,))

# while the promise is being processed
# you can go on and do other work.

while not promise.is_ready():
    # do other work here.
    print('.')

# Check the result of your work
print(promise.get_result())

The PromiseKeeper maintains an internal queue of tasks to be completed. This means you can submit lots of requests for work and it will complete the requests in the order that it recieved them. Here's another example:

#!/usr/bin/env python

from promise_keeper import PromiseKeeper
from random import random
from time import sleep

def slow_square(x: int) -> int:
    sleep(random() * 3)
    return x*x

pk = PromiseKeeper(2)  # use 2 threads this time! (defaults to 1)

# doesn't block
promises = [pk.submit(slow_square, (i,)) for i in range(10)]

# while the promise is being processed
# you can go on and do other work.

while pk.is_running():
    # Again, you can go off and do other things while the tasks are being
    # worked on.
    pass

# Check the result of your work
for promise in promises:
    print(promise.get_result())

As mentioned earlier, the PromiseKeeper will even keep track of exceptions thrown by your code, so you don't have to worry about a crash.

#!/usr/bin/env python

from promise_keeper import PromiseKeeper
from random import random
from time import sleep

def slow_div(x: int, y: int) -> int:
    sleep(random() * 1)
    return x / y

pk = PromiseKeeper(3)
promises = [pk.submit(slow_div, (200, i)) for i in range(-3, 4)]

while pk.is_running():
    pass

for promise in promises:
    if promise.get_exception() is None:
        print(promise.get_result())
    else:
        print(promise.get_exception())

You can even have the PromiseKeeper notify you when your promise is completed rather than having to poll the promises to see when they're done.

#!/usr/bin/env python

from promise_keeper import PromiseKeeper, Promise
from random import random
from time import sleep

def slow_div(x: int, y: int) -> int:
    sleep(random() * 1)
    return x / y

def promise_completed(promise: Promise) -> None:
    if promise.get_exception() is None:
        print("Result", promise.get_result())
    else:
        print("Error", promise.get_exception())

pk = PromiseKeeper(3)
promises = [pk.submit(slow_div, (200, i), notify=promise_completed) \
    for i in range(-3, 4)]

while pk.is_running():
    pass

Metadata

Release files for promise-keeper 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for promise-keeper 1.0.0
File Size Uploaded
promise_keeper-1.0.0.tar.gz 41.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for promise-keeper 1.0.0
File Interpreter ABI Platform
promise_keeper-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 70.3 kB

Release files / promise_keeper-1.0.0.tar.gz

Download URL promise_keeper-1.0.0.tar.gz
Size 41.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a25a4b2c3c3385e550f2ea30896174b69f592ac6ef7f9a0a4a7980dcf6675060
BLAKE2b-256 checksum
How to use checksums
e65a4f55c785824973b34ab451eb3d9f751dfa4ee963b4e97f2d5b61bf01292f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release files / promise_keeper-1.0.0-py3-none-any.whl

Download URL promise_keeper-1.0.0-py3-none-any.whl
Size 29.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4ebfe83ecf839c1d2ee8f11ff78b1e4c440523f6fe6dc4a1839a79e62ed042b
BLAKE2b-256 checksum
How to use checksums
53c182150a6b5e24c70beb98e2a50525f978d35f0bb8c3e7b96446775bee7779
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page