Skip to content

Instantly share code, notes, and snippets.

@njsmith
Last active July 12, 2018 04:00
Show Gist options
  • Save njsmith/aa269020e40cc137936805d5d5bbdd91 to your computer and use it in GitHub Desktop.
Save njsmith/aa269020e40cc137936805d5d5bbdd91 to your computer and use it in GitHub Desktop.
@attr.s
class TrioFuture:
_result = attr.ib(default=None)
_finished = attr.ib(default=attr.Factory(trio.Event))
def set_result(self, result):
assert not self._finished.is_set()
self._result = result
self._finished.set()
async def get(self):
await self._finished.wait()
return self._result.unwrap()
# Sugar
def set_value(self, value):
self.set_result(trio.hazmat.Value(value))
# Sugar
def set_exception(self, exc):
self.set_result(trio.hazmat.Error(exc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment