This document describes the functions and objects used in pyChan.
Base exception class for chan.
Just inherits from Exception
Since ChanClosed inherits from Error, it has the same parameters as Exception, with one addition:
Parameters: | which – Keyword argument, indicating the Chan that was closed. |
---|
Raised when an operation times out.
Chan objects allow multiple threads to communicate.
Parameters: | buflen – Defines the size of the channel’s internal buffer, or 0 if the channel should be unbuffered. An unbuffered channel blocks on all put/get’s, unless a corresponding get/put is already waiting, while a buffered channel will accept puts without blocking as long as the buffer is not full. |
---|
Closes the channel, allowing no further put operations.
Once close is called, the channel allows in-progress put operations to complete and the buffer to clear, and then
Returns True if the channel is closed.
It may be better to call put or get and handle the ChanClosed exception.
Returns an item that was put onto the channel.
get returns immediately if there are items in the channel’s buffer or if another thread is blocked on put. Otherwise, it blocks until another thread puts an item onto this channel.
Parameters: | timeout – An optional floating point number representing the maximum amount of time to block, in seconds. If the timeout expires, then a Timeout error is raised. |
---|---|
Raises : | ChanClosed If the channel has been closed, the buffer is empty, and no threads are waiting on put. |
Places an item onto the channel.
put returns immediately if the channel’s buffer has room, or if another thread is blocked on get. Otherwise, put will block until another thread calls get.
Parameters: |
|
---|---|
Raises : | ChanClosed If the channel has already been closed. |
Returns when exactly one consume or produce operation succeeds.
When this function returns, either a channel is closed, or one value has been pulled from the channels in consumers, or one value has been pushed onto a channel in producers.
chanselect returns different values depending on which channel was ready first:
Parameters: |
|
---|
Here’s a quick example. Let’s say we’re waiting to receive on channels chan_a and chan_b, and waiting to send on channels chan_c and chan_d. The call to chanselect looks something like this:
ch, value = chanselect([chan_a, chan_b],
[(chan_c, 'C'), (chan_d, 'D')])
if ch == chan_a:
print("Got {} from A".format(value))
elif ch == chan_b:
print("Got {} from B".format(value))
elif ch == chan_c:
print("Sent on C")
elif ch == chan_d:
print("Sent on D")
else:
raise RuntimeError("Can't get here")