Vine: Python Promises
Vine is a lightweight Python library that implements 'promises' for managing future evaluations and lazy computations, particularly useful in asynchronous and event-driven programming paradigms. It enhances flexibility by allowing nested promises within callbacks and error handlers. As of version 5.1.0, it is actively maintained by the Celery project.
Warnings
- breaking Python 2.x and older Python 3 versions (3.4, 3.5, 3.6) are no longer supported. Version 5.0.0 dropped support for Python 2.x, 3.4, and 3.5. Version 5.1.0 further dropped Python 3.6, making Python 3.7+ the officially supported range for current releases.
- breaking The `vine.five` module, which contained compatibility shims for Python 2/3, was removed in `vine` 5.0.0a1. Any code directly importing or relying on `vine.five` will break.
- gotcha Dependency conflicts can arise when `vine` is used as a transitive dependency by other libraries like Celery or Kombu. Upgrading `vine` independently may lead to issues such as `ModuleNotFoundError` (e.g., for `vine.five` in older setups) or other unexpected behaviors due to incompatible versions between `vine` and its consumers.
Install
-
pip install vine
Imports
- promise
from vine import promise
- wrap
from vine import wrap
Quickstart
from vine import promise
# Create a promise instance
p = promise()
# Attach a callback for successful completion
def on_success(result):
print(f"Promise fulfilled with: {result}")
p.then(on_success)
# Attach an error handler for failures
def on_error(exc):
print(f"Promise failed with error: {exc!r}")
p.on_error = on_error
# Fulfill the promise with a value
print("Fulfilling promise with 100...")
p(100)
# Expected output: Promise fulfilled with: 100
# Create another promise to demonstrate error
p_error = promise()
p_error.then(on_success) # Attach same success handler
p_error.on_error = on_error # Attach same error handler
# Fulfill the promise with an error
print("\nFulfilling promise with an exception...")
p_error.throw(ValueError("Operation failed!"))
# Expected output: Promise failed with error: ValueError('Operation failed!')