Skip to content

Instantly share code, notes, and snippets.

@mumbleskates
Created May 5, 2019 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mumbleskates/7360a51739cc9562d887356d4270d820 to your computer and use it in GitHub Desktop.
Save mumbleskates/7360a51739cc9562d887356d4270d820 to your computer and use it in GitHub Desktop.
# coding=utf-8
"""Lazy hacks to enable easier usage of async functions, context managers, and iterators in the REPL."""
from asyncio import set_event_loop
try:
import uvloop as loop_maker
except ImportError:
import asyncio as loop_maker
loop = loop_maker.new_event_loop()
set_event_loop(loop)
def aw8(task):
return loop.run_until_complete(task)
class awith(object):
def __init__(self, async_context):
self.wrapped = async_context
def __enter__(self, *args, **kwargs):
return aw8(self.wrapped.__aenter__(*args, **kwargs))
def __exit__(self, *args, **kwargs):
return aw8(self.wrapped.__aexit__(*args, **kwargs))
def aiter(async_iterable):
ait = aw8(async_iterable.__aiter__())
try:
while True:
yield aw8(ait.__anext__())
except StopAsyncIteration:
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment