[Stackless] Mistakes, Corrections and Questions Concerning My Pickle Example Re: cPickle should work?
Andrew Francis
andrewfr_ice at yahoo.com
Mon May 8 20:37:53 CEST 2006
Hello Richard and Colleagues:
>The problem is the order in which the tasklets and
>their channels are unpickled and restored with their
>old state. Where restored is taken to mean setstate
>called on them.
...
Thanks for the detailed explanation for explaining why
the code crashes. I am still going through your
response to get a feel for what is happening. I
thought about what you said.
Subsequently I reviewed my code fragment and did a few
experiments. There are mistakes in my code fragment.
For example, the result of my pickle.load() is not
assigned to anything. Also my version with dumps()
worked because the tasklets are still in memory,
blocked.
Ultimately what I wanted to test, the ability to
pickle tasks waiting on channels, and unpickle them
would not work.
I have included a solution that works. I can't seem to
figure out a way of packaging channel information in
such a way that I can get it from an unpickled task.
Subsequently I pickle channels seperately.
To put things in perspective, I am developing a
scheduler that uses channels to synchronise
"processes" (more like a tasklet group in the sense
that main tasklet creates other tasklets).
An additional feature I want is the ability of the
"scheduler" to pickle and unpickle an individual
"process" blocked on a channel.
>From what I have seen, I can pickle and unpickle
tasklets blocked on channels. So in this regard, I am
happy.
However having a scheduler that is maintaining
tasklets and channels, pickle and unpickle an
individual tasklets is more difficult. Reason: I
cannot do a tasklet.remove() on a blocked tasklet. Is
there something I am missing.
Cheers,
Andrew
~
import stackless
import cPickle as pickle
import sys
"""
Test whether:
There are problems pickling tasklets that are:
1. Created by other tasklets.
2. Blocked on channels.
"""
channels = []
running = []
class Task(object):
def __init__(self, name):
self.name = name
def execute(self, channel):
print "in task ", self.name
print self.name, " about to sleep"
message = channel.receive()
print self.name, " woke up with message",
message
class P(object):
def __init__(self, name, list):
self.name = name
self.tasks = list
self.running = []
self.channels = channels
def execute(self):
print "in task ", self.name
n = 1
for task in self.tasks:
t =
stackless.tasklet(task.execute)(channels[n])
n = n + 1
running.append(t)
t.run()
print self.name, " about to sleep"
message = channels[0].receive()
print self.name, " woke up"
for i in range(0,3):
channels.append(stackless.channel())
p = P("A", [Task("C"),Task("B")])
t = stackless.tasklet(p.execute)()
running.append(t)
t.run()
print "pickling tasks"
#schedule()
f = open("test1.dat","w")
pickle.dump(running,f)
f.close()
print "pickling channels"
f = open("channel.dat","w")
pickle.dump(channels,f)
f.close()
print "done"
~
"""
Unpickle tasks blocked on channels
"""
import stackless
import cPickle as pickle
import sys
channels = []
running = []
class Task(object):
def __init__(self, name):
self.name = name
def execute(self, channel):
print "in task ", self.name
print self.name, " about to sleep"
message = channel.receive()
print self.name, " woke up with message",
message
class P(object):
def __init__(self, name, list):
self.name = name
self.tasks = list
self.running = []
self.channels = channels
def execute(self):
print "in task ", self.name
n = 1
for task in self.tasks:
t =
stackless.tasklet(task.execute)(channels[n])
n = n + 1
running.append(t)
t.run()
print self.name, " about to sleep"
message = channels[0].receive()
print self.name, " woke up"
print "unpickling tasks"
g = open("test1.dat")
tasklets = pickle.load(g)
g.close()
print "unpickling channels"
g = open("channel.dat")
channels = pickle.load(g)
g.close()
print "channels", channels
for channel in channels:
channel.send("Hello")
while stackless.getruncount() > 1:
stackless.schedule()
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless
More information about the Stackless
mailing list