Closed sebastianjanisch closed 5 months ago
Hi @sebastianjanisch,
My guess is that an async generator somehow gets shared between task1 and task2, most probably because both tasks use the same async with self
.
So if both tasks try to enter or exit at the same time, the second task won't be able to access the async generator as it's already running in the first one.
I guess you could easily check that by protecting self
with an async lock and see whether the problem remains. However, the sharing of the async generator is very suspicious so there's probably another problem somewhere else.
I'm not familiar with reflex but I don't think the problem comes from aiostream. My guess is that you would observe the same issue using asyncio.gather(self.task1(), self.task2())
.
Hope this helps, and feel free to re-open the issue if you have more elements pointing towards aiostream.
Hi @vxgmichel thanks for responding so quickly. You are right, I slapped a asyncio lock around the async with self
block (i.e. async with _lock, self:
which did the trick. I'll relay to the guys from Reflex to see if they want to investigate on their side.
Hi,
I am trying to iterate over several async generators using the merge function. I have tried reproducing the behavior I see in a minimal example but so far haven't managed, so I'll try to describe what I observe as best as I can.
I am using aiostream together with the reflex.dev UI library, which allows the launching of background tasks in an async setting (see here: https://reflex.dev/docs/events/background-events/#background-tasks)
Below code is conceptually what i do in my real example, yet it doesn't produce the error I'm seeing.
The setup is that I have several nested async generators which each at some point acquire a lock using
async with self
. In reality these tasks make async httpx calls so it makes sense to run them asynchronously.The error I do observe seems to happen at the
async with self
stage:It's unfortunate that I can't reliably reproduce this but maybe there are thoughts around what the cause might be.