Python & Go

G

Graham Breed

Terry said:
It seems to me that generators are already 'channels' that connect the
calling code to the __next__ method, a semi-coroutine based on the body
of the generator function. At present, the next method waits until an
object is requested. Then it goes into action, yields an object, and
rests again. For parallel operations, we need eager, anticipatory
evaluation that produces things that *will* be needed rather than lazy
evaluation of things that *are* needed and whose absence is holding up
everything else.

Yes, generators look very much like channels. The obvious
thing, from where I'm sitting, is to have a function called
"channel" that takes an iterator, runs it in a different
thread/process/goroutine, and returns an iterator that reads
from the channel. A single threaded version would look very
much like "iter" so let's use iter to get a working example:

#!/usr/bin/python2 -u

channel = iter # placeholder for missing feature

def generate():
i = 2
while True:
yield i
i += 1

def filter(input, prime):
for i in input:
if i%prime != 0:
yield i

ch = channel(generate())
try:
while True:
prime = ch.next()
print prime
ch = channel(filter(ch, prime))
except IOError:
pass

That works fine in a single thread. It's close to the
original go example, hence the evil shadowing of a builtin.
I don't think the "channel" function would present any
problems given an appropriate library to wrap. I got
something like this working with Jython and the E language
but, as I recall, had an accident and lost the code. If
somebody wants to implement it using multiprocessing, go to it!


Graham
 
P

Paul Rubin

sturlamolden said:
A decorator function like @go could just call os.fork and run the
function in the child. We already have a between-process Queue in
multiprocessing to use as channels.

Unlike with interthread queues, you have to serialize the values sent
through those multiprocessing channels. I don't think you can send
functions, generators, file descriptors, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top