Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
Last active December 20, 2023 20:20
Show Gist options
  • Save ValentaTomas/b15f8f815be035b1673d3b6fec27f177 to your computer and use it in GitHub Desktop.
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)
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