[Stackless] Mistakes, Corrections and Questions Re: Stackless Digest, Vol 28, Issue 8

Andrew Francis andrewfr_ice at yahoo.com
Tue May 9 20:43:45 CEST 2006


Message: 2
Date: Tue, 9 May 2006 09:25:54 -0000
From: "Richard" <richard at ccpgames.com>
Subject: Re: [Stackless] Mistakes,	Corrections and
Questions
	Concerning My Pickle Example Re:	cPickleshould work?
To: <stackless at stackless.com>
Message-ID:

<129CEF95A523704B9D46959C922A28000187AC50 at nemesis.central.ccp.cc>
Content-Type: text/plain;	charset="iso-8859-1"

Hi Richard, Christian and Colleagues:

>Well, what I said was more to explain the reason 
>for the crash and why it was happening,

Yes I understand. My code crashed the interpreter and
you pointed out why. However my code is also
conceptually wrong. I feel it is important to state
why my code is wrong. I suspect I still do not have
the whole picture.


>I am not sure I understand.

Assuming I don't  pickle and restore channels, when I
run a modified version of the second code fragment:

print "unpickling tasks"
g = open("test1.dat")
tasklets = pickle.load(g)
g.close()

""""
pickling 
""""
#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()

I will get 

unpickling tasks
channels []

In this scenario I have only unpickled tasklets. I
know the tasklets must have channel references. I
don't know how to get the channels from the tasklets? 
I have come to the conclusion that it is best to
pickle the channels. It may result in a second file,
but that is a minor inconvenience.

>Are you implying that the unpickled 'tasklets' list
>contains the tasklets that are waiting on the
channels
>in the 'channels' list?  That is, the same unpickled
>instances?

Yes I was. However once again, I re-examined my code
and did new tests. I believe I am mistaken on this
one. I probably included a task.insert(). 

>What exactly do you need to get here?

AF> An additional feature I want is the ability of the
AF> "scheduler" to pickle and unpickle an individual
AF> "process" blocked on a channel.

>Isn't this what the 'tasklets' list gives you?
>Unpickled instances of the tasklets which are blocked
>on the channels.  They are blocked where they were on
>the channels they were waiting on, but not linked to
a >channel in any way.

I  would like the ability to remove tasklets that have
been pickled from "memory" until needed again.

Here is an example:

print "pickling tasks"
f = open("taskMay91.dat","w")
pickle.dump(running,f)
f.close()
 
print "pickling channels"
g = open("channelMay91.dat","w")
pickle.dump(c,g)
g.close()
 
#for task in running:
#    task.kill()
    
#f = open("taskMay91.dat")
#unpickled = pickle.load(f)
#f.close()
    
for channel in c:
    channel.send("Hello")

while stackless.getruncount() > 1:
    stackless.schedule()

~

I get:

in task  A
in task  B
B  about to sleep
in task  C
C  about to sleep
A  about to sleep
pickling tasks
pickling channels
A  woke up
B  woke up Hello
C  woke up Hello

The pickling does not remove tasklets and channels
from memory. Not that it should. Now, suppose I  have
pickled tasklets A, B, and C (and their channels) and
I wish to make them candidates for garbage collection.
Sometime in the future, when I need tasklet A, B, and
C, I will unpickle them. 

(a real-life example could be there are tasklets
waiting for an event that will not happen for say,
hours, the tasklets can be pickled until the event
occurs.)

so I include the following:

for task in running:
     task.remove()

and I get

in task 
Traceback (most recent call last):
 A
in task  B
B  about to sleep
in task  C
C  about to sleep
A  about to sleep
pickling tasks

RuntimeError: You cannot remove a blocked tasklet.

Okay. I now try the following:

"""
no harm should be done since I have saved the state of
the tasklets A, B, and C
"""
for task in running:
    task.kill()
    
"""
re-load the tasklets 
"""
f = open("taskMay91.dat")
unpickled = pickle.load(f)
f.close()
    
for channel in c:
    channel.send("Hello")   

and I get the following:

in task  A
in task  B
B  about to sleep
in task  C
C  about to sleep
A  about to sleep
pickling tasks

line 67, in __main__
    channel.send("Hello")
RuntimeError: Deadlock: the last runnable tasklet
cannot be blocked.

I thought the main programme would be the last
runnable tasklet? What I am I doing wrong?

Hopefully I am giving accurate information. I would
not want changes with unpredictable side-effects made
based on faulty information.  I really appreciate your
time.

The last time I tried the Wiki, I could not add
material. I would have happy to summarise the
aforementioned in a Wiki post. I think many readers
would like to know the use and gotchas
associated with pickling tasks.

Cheers,
Andrew






__________________________________________________
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