Open npuichigo opened 10 months ago
Have you tried pyo3_async::asyncio::AsyncGenerator::from_stream(AllowThreads(map()))
?
I've just released the version 0.3.2, because AllowThreads
was bugged. Anyway, as you know, the crate is deprecated, so I invite you to use the PyO3 branches I mentioned in #2. I know there is no Stream
support planned for now in PyO3, but I should add it soon in a new PR.
It works well and no dead lock occurs. I'm not quite sure the essential difference.
As written in the stack traces you shown, flume
uses a mutex, while tokio::sync::mpsc
don't.
Your deadlock seems to be the following:
I've looked at flume implementation, and indeed, the channel lock is held for the wakeup (I don't understand why honestly).
By using a intermediate tokio task with a mpsc, the flume
channel doesn't wakeup the async generator __next__
coroutine but the tokio task, so it doesn't need to acquire the GIL.
Your use case is interesting though, because you're the second one I will to make a feedback on the async support in PyO3, and both of us encountered a deadlock. So it may indicate that the issue may be more common as I expected and should require a dedicated documentation. I will talk with PyO3 maintainers about that.
Thanks for your detailed reply. I will have a try with PyO3 master. One question is how is the performance? I don't which is better:
AllowThreads
with stream which may have dead lock with GIL.mpsc channel
.Since I would like to let Python user randomly combine the exposed stream algorithm, I need to make sure the final one will always avoid dead lock.
Recently, I encountered a same issue mentioned here related to dead lock https://github.com/PyO3/pyo3/pull/3540#issuecomment-1789623562
Here I use the
par_stream
crate to provide a parallel async stream implementation and wrap that withpyo3-async
. Thepar_map
returns a flumeRecvStream
which internally has a lock for thequeue
.Now it has a dead lock and hangs in Python.
I have no idea where to add
allow_thread
here, just adding toparallel_stream
seems not work.BTW, if I change to use a
tokio mpsc channel
like this:It works well and no dead lock occurs. I'm not quite sure the essential difference.