77
88#{ Classes
99class Channel (object ):
10- """A channel is similar to a system pipe . It has a write end as well as one or
10+ """A channel is similar to a file like object . It has a write end as well as one or
1111 more read ends. If Data is in the channel, it can be read, if not the read operation
1212 will block until data becomes available.
1313 If the channel is closed, any read operation will result in an exception
@@ -51,18 +51,23 @@ def write(self, item, block=True, timeout=None):
5151 :param block: If True, the call will block until there is free space in the
5252 channel
5353 :param timeout: timeout in seconds for blocking calls.
54- :raise IOError: when writing into closed file or when writing into a non-blocking
55- full channel
54+ :raise IOError: when writing into closed file
55+ :raise EOFError: when writing into a non-blocking full channel
5656 :note: may block if the channel has a limited capacity"""
5757 if self ._closed :
5858 raise IOError ("Cannot write to a closed channel" )
5959
6060 try :
6161 self ._queue .put (item , block , timeout )
6262 except Full :
63- raise IOError ("Capacity of the channel was exeeded" )
63+ raise EOFError ("Capacity of the channel was exeeded" )
6464 # END exception handling
6565
66+ def size (self ):
67+ """:return: approximate number of items that could be read from the read-ends
68+ of this channel"""
69+ return self ._queue .qsize ()
70+
6671 def close (self ):
6772 """Close the channel. Multiple close calls on a closed channel are no
6873 an error"""
@@ -86,22 +91,42 @@ def __init__(self, wchannel):
8691
8792 #{ Interface
8893
89- def read (self , block = True , timeout = None ):
90- """:return: an item read from the channel
94+ def read (self , count = 0 , block = True , timeout = None ):
95+ """read a list of items read from the channel. The list, as a sequence
96+ of items, is similar to the string of characters returned when reading from
97+ file like objects.
98+ :param count: given amount of items to read. If < 1, all items will be read
9199 :param block: if True, the call will block until an item is available
92100 :param timeout: if positive and block is True, it will block only for the
93101 given amount of seconds.
94- :raise IOError: When reading from an empty channel ( if non-blocking, or
95- if the channel is still empty after the timeout"""
102+ :return: single item in a list if count is 1, or a list of count items.
103+ If the channel was empty and count was 1, an empty list will be returned.
104+ If count was greater 1, a list with less than count items will be
105+ returned.
106+ If count was < 1, a list with all items that could be read will be
107+ returned."""
96108 # if the channel is closed for writing, we never block
97109 if self ._wc .closed :
98110 block = False
99-
111+
112+ out = list ()
100113 try :
101- return self ._wc ._queue .get (block , timeout )
114+ if count == 1 :
115+ out .append (self ._wc ._queue .get (block , timeout ))
116+ elif count < 1 :
117+ while True :
118+ out .append (self ._wc ._queue .get (block , timeout ))
119+ # END for each item
120+ return out
121+ else :
122+ for i in xrange (count ):
123+ out .append (self ._wc ._queue .get (block , timeout ))
124+ # END for each item
125+ # END handle count
102126 except Empty :
103- raise IOError ("Error reading from an empty channel" )
104- # END handle reading
127+ pass
128+ # END handle exceptions
129+ return out
105130
106131 #} END interface
107132
0 commit comments