[Stackless] deepcopying (pickling) channels

lars van Gemerden lars at rational-it.com
Wed Oct 2 11:47:13 CEST 2013


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):
        stackless.tasklet(self.sender)()
        stackless.tasklet(self.receiver)()
        stackless.run()

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

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

if __name__ == "__main__":

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

    t = Test()
    t.run()
    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

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

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

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

    @classmethod
    def tasklet(cls, func):
        return stackless.tasklet(func)
----------------------------------------------------------------------------------------------

Cheers, Lars
-- 
====================================
Lars van Gemerden
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/20131002/fb89bbfb/attachment.html>


More information about the Stackless mailing list