简体   繁体   中英

resolve <async_generator object …>

I have a Function that yields:

<async_generator object functionOne at 0x000001CC289F8D30>

Is it possible to resolve the async_generator object, such that the array it contains is accessible?

There is no any array behind it (it's a generator, lazy by design), you should iterate this object with async for or manually, using __aiter__() and __anext__() . This probably will work for you:

async_generator_iterator = <your_async_generator_function>()
res = [i async for i in async_generator_iterator]

if you want to evaluate that outside of async context, just wrap it with async function and run in event loop:

async def get_result_async(asyncgen):
    return [i async for i in asyncgen]

def get_result_sync(asyncgen):
    loop = asyncio.get_event_loop()
    coro = get_result_async(asyncgen)
    res = loop.run_until_complete(coro)
    return res
    
async def functionOne():
    yield 1
    yield 2
    yield 3

asyncgen = functionOne()
get_result_sync(asyncgen)

# => [1, 2, 3]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM