# HG changeset patch # User paulb # Date 1168811791 0 # Node ID 06a385740b93c504c9c2f7522d47086c0ff4432f # Parent 0f1359fd3b783ce85168edb39824d190703eae80 [project @ 2007-01-14 21:56:31 by paulb] Converted the example to use the newer add_wait, wait and finish methods on an Exchange subclass. diff -r 0f1359fd3b78 -r 06a385740b93 examples/PyGmy/ppygmy.py --- a/examples/PyGmy/ppygmy.py Sun Jan 14 19:11:03 2007 +0000 +++ b/examples/PyGmy/ppygmy.py Sun Jan 14 21:56:31 2007 +0000 @@ -365,36 +365,34 @@ """ image = Image.new("RGB", (self.width,self.height)) - draw = ImageDraw.Draw(image) - total = self.width*self.height - count = 0 - nproc = 0 - - exchange = pprocess.Exchange() - y = 0 - nproc = len(exchange.active()) - - while y < self.height or nproc > 0: - if y < self.height and nproc < limit: - channel = pprocess.start(self.render_row, y) - exchange.add(channel) - y += 1 + exchange = PyGmyExchange(limit=limit) + exchange.draw = ImageDraw.Draw(image) + exchange.total = self.width*self.height + exchange.count = 0 - for channel in exchange.ready(): - sy, row = channel.receive() - sx = 0 - for col in row: - draw.point((sx,sy),fill=(col.x*255,col.y*255,col.z*255)) - #depth=depth/self.backplane - #draw.point((sx,sy),fill=(depth*255,depth*255,depth*255)) - count=count+1 - sx += 1 + for y in range(0, self.height): + channel = pprocess.start(self.render_row, y) + exchange.add_wait(channel) - percentstr = str(int((count/float(total))*100))+"%" - print "\b\b\b"+percentstr - - nproc = len(exchange.active()) - + exchange.finish() image.save(filename) +class PyGmyExchange(pprocess.Exchange): + + "A convenience class for parallelisation." + + def store_data(self, channel): + + "Store the data arriving on the given 'channel'." + + sy, row = channel.receive() + sx = 0 + for col in row: + self.draw.point((sx,sy),fill=(col.x*255,col.y*255,col.z*255)) + self.count = self.count + 1 + sx += 1 + + percentstr = str(int((self.count/float(self.total))*100))+"%" + print "\b\b\b"+percentstr + # vim: tabstop=4 expandtab shiftwidth=4