[Stackless] Questions about the Tasklet _channel attribute
Andrew Francis
andrewfr_ice at yahoo.com
Sat Aug 30 19:20:31 CEST 2008
Hi Christian:
--- On Fri, 8/29/08, Christian Tismer <tismer at stackless.com> wrote:
> For some reason, this is something I always tried to
> prevend.
> The channel logic is not meant to be manipulated.
> If this is possible via pickling, then it is considered a
> bug.
I have included some sample code illustrating attaching and detaching tasklets to and from channels. It involves __reduce__()
and __setState__ as per the instructions on the Stackless wiki. Christian, is it stuff like this that you have objections?
That said, I believe that the ability to detach a tasklet blocked on a channel and reattaching it to a new channel is a useful feature. It opens up possibilities like process migration and swapping out of memory groups of tasklets that have been waiting a long time for I/O. The later is of particular use to me.
Since I have more time, I will write more useful examples of this feature in the weeks to come.
Cheers,
Andrew
"""
serialize.py
Andrew Francis
<song>Jeremy - Pearl Jam </song>
The purpose of this programme is to illustrate how tasklets can
be attached an detached from channels
"""
import stackless
def detach(channel, tasklet):
x, y, (balance, flags, tasklets) = channel.__reduce__()
tasklets.remove(tasklet)
channel.__setstate__((balance, flags, tasklets))
return channel
def attach(channel, tasklet):
x, y, (balance, flags, tasklets) = channel.__reduce__()
tasklets.append(tasklet)
channel.__setstate__((balance, flags, tasklets))
return channel
class Process(object):
def __init__(self, channel):
self.channel = channel
def execute(self):
print "my channel:", self.channel
print self.channel.receive()
if __name__ == "__main__":
channel1 = stackless.channel()
channel2 = stackless.channel()
p = Process(channel1)
t = stackless.tasklet(p.execute)()
stackless.schedule()
channel1 = detach(channel1, t)
p.channel = channel2 = attach(channel2, t)
channel2.send("Finish")
stackless.run()
More information about the Stackless
mailing list