Coroutines

A module for generic coroutines.

Coroutine Decorator

This is a decorator used to start a coroutine.

coroutine(func) A decorator to start coroutines

Example Use:

@coroutine
def printer(target):
    while True:
        output = (yield)
        print output
        target.send(output)

@coroutine
def sink(filename):
    out_file = open(filename, 'w')
    while True:
        output = (yield)
        out_file.write(output)

s = sink("test.txt")
p = printer(s)
for line in output:
    p.send(line)

The Broadcast

A coroutine to send the same input to multiple targets.

digraph broadcast {
s -> B
B -> t1
B -> t2
B -> t3
}

Example Use:

s1 = sink('out_1.txt')
s2 = processor('out_2.csv')
b = broadcast((s1, s2))
p = printer(b)
for line in source:
    p.send(line)
  • Here we re-use sink and printer from the previous example and assume the existence of a processor co-routine that transforms the input to comma-separated-values.
broadcast(*args, **kwargs)

The Comma Join

This coroutine reads in a number of inputs before joining them with a comma and sending the string down the pipeline.

comma_join(*args, **kwargs)

digraph comma_join {
source -> processor1
source -> processor2
source -> processor3
processor1 -> comma_join
processor2 -> comma_join
processor3 -> comma_join
comma_join -> target
}

Although not evident from the graph, since this is a couroutine I assume that the processors are always called in the same order if the output needs it.

Output Coroutine

output(*args, **kwargs)

digraph output {
source -> pipeline
pipeline -> output
}

The output does not take a co-routine as an argument so it has to act as a sink.

Comma Append

Takes a stream of input strings and appends strings sent to it to each string (acting like a zip between a generator and an input stream).

comma_append(*args, **kwargs)

File Output Coroutine

The file_output acts much like the output co-routine but assumes that the target is a disk-file and will create it if passed a string instead of an open file.