mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Add an example application to the docs.
This commit is contained in:
parent
6fbf703fa2
commit
f5f9a370d4
1 changed files with 31 additions and 2 deletions
|
@ -128,5 +128,34 @@ IndexError: pop from an empty deque
|
|||
>>> d.extendleft('abc') # extendleft() reverses the input order
|
||||
>>> d
|
||||
deque(['c', 'b', 'a'])
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
A roundrobin task server can be built from a \class{deque} using
|
||||
\method{popleft()} to select the current task and \method{append()}
|
||||
to add it back to the tasklist if the input stream is not exhausted:
|
||||
|
||||
\begin{verbatim}
|
||||
def roundrobin(*iterables):
|
||||
pending = deque(iter(i) for i in iterables)
|
||||
while pending:
|
||||
task = pending.popleft()
|
||||
try:
|
||||
yield task.next()
|
||||
except StopIteration:
|
||||
continue
|
||||
pending.append(task)
|
||||
|
||||
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
||||
print value
|
||||
|
||||
a
|
||||
d
|
||||
e
|
||||
b
|
||||
f
|
||||
c
|
||||
g
|
||||
h
|
||||
|
||||
\end{verbatim}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue