Sunday, August 17, 2008

Stackless Python serialize test

You can pickle the Python execution state with Stackless Python.


import stackless
def a():
c = 1
while(True):
print c
c += 1
stackless.schedule()

tskOrg = stackless.tasklet(a)()

print "----first run"
stackless.schedule()
stackless.schedule()
stackless.schedule()

print "----now serialize the task"
import pickle
p = pickle.dumps(tskOrg)

print "----second run"
stackless.schedule()
stackless.schedule()
stackless.schedule()

print "----remove the task from the scheduler and insert the pickled one"
tskOrg.remove()
tskCopy = pickle.loads(p)
tskCopy.insert()

print "----run the pickled task"
stackless.schedule()
stackless.schedule()
stackless.schedule()

----first run
1
2
3
----now serialize the task
----second run
4
5
6
----remove the task from the scheduler and insert the pickled one
----run the pickled task
4
5
6


Note:
Stackless Python is not a standard Python.
It's a variant of Python that doesn't use C stack for Python function calls.
You need to download it from http://www.stackless.com/
and customize Python installed on your machine.

No comments: