Find answers from the community

s
F
Y
a
P
Updated last month

Async

Does anyone know how to run async indexing within fastapi (I get a complaint that you can’t use async up in an existing event loop)
L
R
16 comments
You could do

Plain Text
import nest_asyncio
nest_asyncio.apply()


But you'll need to set the loop type
unicorn.run(..., loop="asyncio")

The issue is python constructors can't be async (I think)
I deep dived into the library and I think it’s because the async code to do embedding uses async.run not gather which would utilise the same event loop that fastapi already uses
Where does it use asyncio.run() here? I would expect that only to be called from a syncronous function 🤔
VectorStoreIndex -> build_index_from_nodes -> run_async_tasks
ah yes -- the constuctor can't be async, so it runs async tasks under a sync endpoint :PSadge:
Hmm this affects transformations for metadata extractors too- which I guess must also be async and have an async.run buried at the bottom of them
transformations are async first, and use asyncio.run() to run synchronously
I would run all my tranformations first, then insert nodes
nodes = await transformation.acall(documents)
nodes = await transformation.call(nodes)
...
I’ll have to do that- was just using from_documents(…transformations=[…
I think as long as they do async.run and not gather, I can’t call them from the existing fastapi loop period. I may be wrong?
That's not correct. You can do uvicron.run(..., loop="asyncio") and add
Plain Text
import nest_asyncio 
nest_asyncio.apply()


To your code
🔥will give it a go
Add a reply
Sign up and join the conversation on Discord