How could I implement the same as Python's async queue?

Python has an async queue in its standard library.

It's super easy to use. Here's some example code: Queues — Python 3.10.0 documentation

Essentially I create a queue and then async functions may await the queue.get for a message. The queue.get returns None after waiting for a timeout period.

Producer async functions may add to the queue via queue.put.

This seems an extremely straightforward model - async functions put things on the queue and async functions await gets from the queue.

How would I do the same in Rust? I've looked at std::sync::mpsc - Rust but it wasn't obvious to me after several reads if this maps onto the logic flow for the Python async queue.

Any help appreciated. Not looking for anyone to write the code, just a point in the right direction.

There isn't a 1:1 equivalent to that queue in Rust - but in 99% of cases that you'd want it Tokio's mpsc channel (or std's if you're using non-async code) will work fine. Unlike Python's queue, it separates the sender and receiver into two separate values. This usually maps on nicely to program structure, but you can also store the receiver and a sender alongside each other in a struct if you want to both send and receive. Instead of put and get you use send on the sender and recv on the receiver.


To implement a 1:1 version of Python's queue, you could base it off of async-channel - just create a struct Queue<T> that contains both a sender and a receiver. Seems like you wouldn't be able to implement join or task_done as easily though.


Interesting - it looks similar. Any idea how I would ensure that recv/wait gives up after a timeout period? I can't see a timeout argument there.

Compose it with a function like tokio::time::timeout


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.