Created
August 28, 2024 08:48
-
-
Save jph00/ebcfdd3b017605ae5b1c61c238bfda09 to your computer and use it in GitHub Desktop.
A possibly useful async2sync convertor
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 | |
from inspect import iscoroutine, isasyncgen | |
from concurrent.futures import ThreadPoolExecutor | |
import nest_asyncio | |
nest_asyncio.apply() | |
def _iter_coro(r): | |
loop = asyncio.get_event_loop() | |
if loop.is_running(): | |
with ThreadPoolExecutor() as ex: | |
try: | |
while True: yield ex.submit(loop.run_until_complete, r.__anext__()).result() | |
except StopAsyncIteration: return | |
def run_coro(r): | |
"Run coroutine `r` if it is one, otherwise just return `r` directly" | |
if isasyncgen(r): return _iter_coro(r) | |
if not iscoroutine(r): return r | |
loop = asyncio.get_event_loop() | |
if loop.is_running(): | |
with ThreadPoolExecutor() as ex: return ex.submit(asyncio.run, r).result() | |
return loop.run_until_complete(r) | |
async def g(): | |
for i in range(10): yield i | |
for i in run_coro(g()): print(i, end=' ') | |
async def f(msgs:list): | |
async with AsyncAnthropic().messages.stream(model=models[1], messages=mk_msgs(msgs), max_tokens=20) as s: | |
async for o in s.text_stream: yield o | |
for o in run_coro(f('hi')): print(o, end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment