Introduction
This Python project provides a concurrency framework using promises, enabling asynchronous execution of tasks with support for both threading and multiprocessing. It allows for easy task execution management, result handling, and synchronization of concurrent operations without delving into the complexities of threading or multiprocessing modules directly.
Features
- Simple API: Intuitive and easy-to-use API for managing asynchronous task executions.
- Concurrency Modes: Supports both threading and multiprocessing to cater to different use cases.
- Promise-Based: Utilize promises for handling asynchronous execution results and errors.
- Event Loop: Central event loop for managing and synchronizing promise lifecycles.
- Decorators: Simplify asynchronous function execution with decorators.
Installation
This project does not require installation of external packages. Ensure you have Python 3.8 or newer installed on your system, as this project utilizes features introduced in Python 3.8.
To install it, run the following command:
pip install promisipy
Usage
Basic Concepts
- Promise: Represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
- Event Loop: Manages the lifecycle of promises, including registration, execution, and unregistration.
promisipyDecorator: Converts a regular function into one that returns a promise when called, executing the function in a separate thread or process.
Creating a Promise
To create a promise, instantiate a Promise object with a target function. Specify the concurrency mode ("threading" or "multiprocessing") according to your needs.
Waiting for a Promise to Resolve
Use the wait method on a promise object to block until the promise has resolved. This method returns a Promise.Resolution object containing the result or error.
Executing Functions Asynchronously
Decorate functions with @promisipy(mode="threading") or @promisipy(mode="multiprocessing") to run them asynchronously as promises.
Examples
Basic Promise Execution
from promisipy import Promise, promisipy
# Function to execute asynchronously
def task():
return "Result of async task"
# Create and start a promise
promise = Promise(execution=task, mode="threading").start()
# Wait for the promise to resolve
resolution = promise.wait()
print("Result:", resolution.result)
Using promisipy Decorator
from promisipy import promisipy
@promisipy(mode="threading")
def async_task():
return "Result from decorated async task"
# The function now returns a promise
promise = async_task()
# Wait for the promise to resolve and print the result
print("Result:", promise.wait().result)
Waiting for Multiple Promises
from promisipy import Promise, promisipy
# Define multiple tasks
def task1():
return "Result of task 1"
def task2():
return "Result of task 2"
# Create promises for each task
promise1 = Promise(execution=task1, mode="threading").start()
promise2 = Promise(execution=task2, mode="threading").start()
# Wait for all promises to resolve
results = Promise.all([promise1, promise2])
print("Results:", [res.result for res in results])
This framework is versatile and can be adapted for various asynchronous execution needs, simplifying the handling of concurrent operations in your Python applications.
Metadata
Release files for promisipy 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| promisipy-0.1.3.tar.gz | 5.2 kB | Without provenance | Details for promisipy-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| promisipy-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for promisipy-0.1.3-py3-none-any.whl |
Total release size: 9.5 kB
Release files / promisipy-0.1.3.tar.gz
| Download URL | promisipy-0.1.3.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0036e7b335d6336b4bf64596a5db118ea71103cde0b11b695b122e288b1a53ba
|
|
BLAKE2b-256 checksum How to use checksums |
cb4bd14ceca86a5c727f3efc380f8ad3d6e2f6f85d4cc1eeb5bfc295614d1211
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / promisipy-0.1.3-py3-none-any.whl
| Download URL | promisipy-0.1.3-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e54a0f62e29505248ff5ca25694cb712e3d78c842c0636e77c8d0982c30bc780
|
|
BLAKE2b-256 checksum How to use checksums |
0c7ac1c2f5e53bffe078f3b8dc844cd5c60653e263894a31defb6776024a28e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for promisipy 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.