pyChan: Go-style channels for Python

Release v0.2.

pyChan implements Go’s chan type in Python, making concurrent programming in Python simple.

Install using pip install chan

Source code at

API Reference


You can use Chan.put() to place items onto a Chan, and Chan.get() to receive them:

c = Chan()

# Thread 1

# Thread 2
print "Heard: %s" % c.get()

Channels can be closed (usually by the sender). Iterating over a channel gives all values until the channel is closed

c = Chan()

# Thread 1

# Thread 2
for thing in c:
    print "Heard:", thing

You can wait on multiple channels using chanselect(). Pass it a list of input channels and another of output channels, and it will return when any of the channels is ready

def fan_in(outchan, input1, input2):
    while True:
        chan, value = chanselect([input1, input2], [])
        if chan == input1:
            outchan.put("From 1: " + str(value))
            outchan.put("From 2 " + str(value))

You can see more examples in the “examples” directory.


Indices and tables

Project Versions

Table Of Contents

Next topic

API Reference

This Page