[Python-ideas] channel (synchronous queue) (original) (raw)

Matt Joiner anacrolix at gmail.com
Sat Feb 18 16:38:06 CET 2012


Recently (for some) the CSP style of channel has become quite popular in concurrency implementations. This kind of channel allows sends that do not complete until a receiver has actually taken the item. The existing queue.Queue would act like this if it didn't treat a queue size of 0 as infinite capacity.

In particular, I find channels to have value when sending data between threads, where it doesn't make sense to proceed until some current item has been accepted. This is useful when items are not purely CPU bound, and so generators are not appropriate.

I believe this rendezvous behaviour can be added to queue.Queue for the maxsize=0 case, with maxsize=None being the existing "infinite queue" behaviour. Additionally a close method, Closed exception and other usability features like an iter for receiving until closed can be added. The stackless class linked below also has some other possible ideas for performance reasons that make a lot of sense.

Existing code using queue.Queue would remain completely unaffected by such additions if the default maxsize value is changed to maxsize=None, and maxsize=0 is not being explicitly passed (it's currently the default).

Here are a few links for some background and ideas: http://gevent.org/gevent.queue.html#gevent.queue.Queue http://www.disinterest.org/resource/stackless/2.6-docs-html/library/stackless/channels.html#the-channel-class http://en.wikipedia.org/wiki/Communicating_sequential_processes#Comparison_with_the_Actor_Model http://golang.org/doc/go_spec.html#Channel_types



More information about the Python-ideas mailing list