pythonstacklesspython-stackless

In stackless Python, can you send a channel over a channel?


I do not have stackless currently running, so I can not try this myself.

import stackless
ch1 = stackless.channel()
ch2 = stackless.channel()

ch1.send(ch2)
ch3 = ch1.receive()

Are ch2 and ch3 then the same channel? Say:

text = "Hallo"
ch2.send(text)
assert text == ch3.receive()

This feature reminded me of a talk about Newsqueak that Robert Pike (of Plan9 fame) gave at Google. In Newsqueak you could send channels over channels.


Solution

  • Yes. Just tested.

    >>> import stackless
    >>> ch1 = stackless.channel()
    >>> def a():
    ...  ch2 = stackless.channel()
    ...  ch1.send(ch2)
    ...  ch2.send("Hello")
    ...
    >>> def b():
    ...  ch3 = ch1.receive()
    ...  print ch3.receive()
    ...
    >>> stackless.tasklet(a)()
    <stackless.tasklet object at 0x01C6FCB0>
    >>> stackless.tasklet(b)()
    <stackless.tasklet object at 0x01C6FAB0>
    >>> stackless.run()
    Hello