PEP 255: Simple Generators

Roman Suzi rnd at onego.ru
Wed Jun 20 06:08:05 EDT 2001


Pipes instead of generators:

q = openpipe()   

def g(pipe):
  for i in range(10):
    pipe.put(i)        # this brings g into sleep till pipe is read
  raise StopIter       # this will be raised where reading from pipeoccurs

g(q)  # here we call function with stops for sending to pipe

for gg in q():
  if cond(gg):
     print gg
  else:
     q.close()         # kill q
     break
  
There could be more objects/functions registered for certain pipe.

Of course, parallelly working objects are even better, but 
this approach also works and doesn't restrict itself to generators.

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list