[Stackless] deepcopying (pickling) channels

Kristján Valur Jónsson kristjan at ccpgames.com
Wed Oct 9 12:52:48 CEST 2013

Hi, I just tried reproducing this with the current stackless 2.7 and it just works.
What stackless are you using
One of the points of stackless is that it can copy/pickle modules just fine.
instead of copy, deepcopy, can you try:

import pickle
print pickle.dumps(t)

and see if this succeeds?

Ø  Probably a lack in my basic understanding of stackless: why is the line  t.channel = None reached if one of the tasklets is still waiting on a channel?
stackless.run() returns when there are no runnable tasklets.  One tasklet is blocked, the other is spent.  So, stackless.run returns.


From: stackless-bounces at stackless.com [mailto:stackless-bounces at stackless.com] On Behalf Of lars van Gemerden
Sent: 2. október 2013 09:47
To: stackless at stackless.com
Subject: [Stackless] deepcopying (pickling) channels

Hi all,

I am getting a strange (for me) exception when i try to deepcopy/pickle channels that are still sending or receiving, as in:


import stackless, copy

class Test(object):
    def __init__(self):
        self.channel = stackless.channel()

    def run(self):

    def sender(self):
        counter = 0
        while counter < 10:
            counter += 1

    def receiver(self):
        counter = 0
        while counter < 12:
            counter = self.channel.receive()
            print counter,
        print "done"

if __name__ == "__main__":

    t = Test()
    t.channel = None
    t = copy.deepcopy(t) #OK

    t = Test()
    t = copy.deepcopy(t) #ERROR

the error is:

TypeError: object.__new__(NotImplementedType) is not safe, use NotImplementedType.__new__()

somewhere deep into deepcopy, after reading some forums, i think it's trying to copy a module.

Probably a lack in my basic understanding of stackless: why is the line  t.channel = None reached if one of the tasklets is still waiting on a channel?

Anyway,in this case the receiver tasklet is still waiting while the sender is done, but it also happens if the receiever is done before the sender (e.g. change 12 to 8). There is no error if both are done at the same time (change 12 to 10).

Can someone explain this to me? Is it unavoidable or can it somehow be fixed. Otherwise i would probably need to clean up all channels after a stackless run is finished and create them before a run starts or do something similar in in __getstate__/__setstate__. I use the ??? pattern to allow external interuption the stackless run:


class OpenTasklets(Tasklets):

    stop = False

    def run(cls, maxcount = None):
        cls.schedule_channel = stackless.channel()
        cls.schedule_channel.preference = 1
        counter = 0
        while stackless.getruncount() != 1:
            cls.reschedule(cls.stop or (maxcount and counter > maxcount))
            counter += 1

    def schedule(cls):
        if cls.schedule_channel.receive():
            raise TaskletExit

    def reschedule(cls, stop = False):
        while cls.schedule_channel.balance < 0:

    def tasklet(cls, func):
        return stackless.tasklet(func)

Cheers, Lars
Lars van Gemerden
lars at rational-it.com<mailto:lars at rational-it.com>
+31 6 26 88 55 39
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20131009/f9d3d9b0/attachment.html>

More information about the Stackless mailing list