Skip to main content

This Python library provides a flexible and easy-to-use asynchronous programming model based on the concept of promises, similar to those found in JavaScript.

Project description

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

  1. Promise: Represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
  2. Event Loop: Manages the lifecycle of promises, including registration, execution, and unregistration.
  3. promisipy Decorator: 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.

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

promisipy-0.1.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

promisipy-0.1.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file promisipy-0.1.3.tar.gz.

File metadata

  • Download URL: promisipy-0.1.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for promisipy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0036e7b335d6336b4bf64596a5db118ea71103cde0b11b695b122e288b1a53ba
MD5 6920315f7a0d0c2678a5ce8c12b27be5
BLAKE2b-256 cb4bd14ceca86a5c727f3efc380f8ad3d6e2f6f85d4cc1eeb5bfc295614d1211

See more details on using hashes here.

File details

Details for the file promisipy-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: promisipy-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for promisipy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e54a0f62e29505248ff5ca25694cb712e3d78c842c0636e77c8d0982c30bc780
MD5 f35bc2836c82fce4898d11f9bcfd0d4d
BLAKE2b-256 0c7ac1c2f5e53bffe078f3b8dc844cd5c60653e263894a31defb6776024a28e1

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