Skip to main content

A simple async processing helper

Project description

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

Project details


Download files

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

Source Distribution

promise_keeper-1.0.0.tar.gz (41.1 kB view details)

Uploaded Source

Built Distribution

promise_keeper-1.0.0-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file promise_keeper-1.0.0.tar.gz.

File metadata

  • Download URL: promise_keeper-1.0.0.tar.gz
  • Upload date:
  • Size: 41.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for promise_keeper-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a25a4b2c3c3385e550f2ea30896174b69f592ac6ef7f9a0a4a7980dcf6675060
MD5 680237ea26bc6c8bf69f7178edeb6a7e
BLAKE2b-256 e65a4f55c785824973b34ab451eb3d9f751dfa4ee963b4e97f2d5b61bf01292f

See more details on using hashes here.

File details

Details for the file promise_keeper-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for promise_keeper-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4ebfe83ecf839c1d2ee8f11ff78b1e4c440523f6fe6dc4a1839a79e62ed042b
MD5 8314dd5a1b61fbc0840f83526f4d0eee
BLAKE2b-256 53c182150a6b5e24c70beb98e2a50525f978d35f0bb8c3e7b96446775bee7779

See more details on using hashes here.

Supported by

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