Last active
December 20, 2023 20:20
-
-
Save ValentaTomas/b15f8f815be035b1673d3b6fec27f177 to your computer and use it in GitHub Desktop.
Deferred future for controlling async flow (there are existing asyncio features for doing this)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import json | |
from typing import TypeVar, Generic, List | |
T = TypeVar("T") | |
class DeferredFuture(Generic[T]): | |
def __init__(self): | |
self.future = asyncio.Future[T]() | |
def __call__(self, result: T): | |
if not self.future.done(): | |
self.future.set_result(result) | |
def __await__(self): | |
result = yield from self.future.__await__() | |
return result | |
def reject(self, reason: Exception): | |
if not self.future.done(): | |
self.future.set_exception(reason) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment