Implementing Javascript-like async Promise in Python for Eventlet-based coroutines.
Project description
Eventlet Promise
This package implements a Javascript-like Promise to use for eventlet threads.
from typing import Callable
import eventlet as hub
from eventlet_promise.promise import Promise
def executor_(resolveFunc : Callable[[Any], None], rejectFunc : Callable[[Any], None]):
t1, t2 = 5, 6
# print(t1 := 10 * random())
hub.spawn_after(t1, lambda: print('\tResolving') or resolveFunc(t1))
hub.spawn_after(t2, lambda: print('\tRejecting') or rejectFunc(TimeoutError("Timed out")))
# promise = Promise(None)
promise = Promise(executor_)
new_promise = promise.then()
attached = Promise.resolve(new_promise).then(lambda x: x + 1).then(lambda x: x + 1)
p1 = Promise.resolve(1).then(2).then()
p2 = Promise.reject(1).then(2, 2).then().then()
p3 = p1.then()
p_all = Promise.all([p1, p3, new_promise, promise])
p_settled = Promise.allSettled([p1, p2, p3, new_promise, promise])
p_any = Promise.any([p2, promise.then(), new_promise, promise])
p_race = Promise.race([new_promise.then(), new_promise.then()])
while True:
print(promise)
print(new_promise)
print(attached)
print(p1)
print(p2)
print(p3)
print('all', p_all)
print('allSettled', p_settled)
print('any', p_any)
print('race', p_race)
hub.sleep(0) # to allow other eventlet coroutines to work.
hub.sleep(2) # so that you don't see all scroll by.
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
eventlet_promise-0.5.1.tar.gz
(6.5 kB
view hashes)
Built Distribution
Close
Hashes for eventlet_promise-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23ca4dbe113998dd0934e6a17ad49eabf4a7de9d51de8b7319ac37fe0620ae62 |
|
MD5 | c649b57aa4ef261cc8e20367bc5d8f5c |
|
BLAKE2b-256 | 466e5670af74d976b77b8a78b6b239a2602c7c0d792ce9b542315f7410f168d4 |