In unmanaged Python, can you send pipe by pipe?

I do not have stacking at this time, so I can not try it myself.

import stackless ch1 = stackless.channel() ch2 = stackless.channel() ch1.send(ch2) ch3 = ch1.receive() 

Are ch2 and ch3 the same channel? Say:

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

This feature reminded me of a story about Newsqueak that Robert Pike ( Plan9 is known by Google. In Newsqueak, you can send feeds through feeds.

+4
source share
2 answers

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 
+4
source

Channels send regular Python links, so the data you send (channel, string, whatever) is exactly what you received.

One example of sending a channel on a channel is when you use the tasklet as a service, i.e. the tasklet listens for the request channel, does the work, and returns the result. The request must include data for operation and a return channel for the result, so that the result is transmitted to the requestor.

Here is an extreme example that I developed for my Stackless talk at PyCon a few years ago. This creates a new task for every function call, so I can use a recursive factorial implementation that does not need to worry about the Python stack limit. I allocate a tasklet for each call, and it receives a return channel for the result.

 import stackless def call_wrapper(f, args, kwargs, result_ch): result_ch.send(f(*args, **kwargs)) # ... should also catch and forward exceptions ... def call(f, *args, **kwargs): result_ch = stackless.channel() stackless.tasklet(call_wrapper)(f, args, kwargs, result_ch) return result_ch.receive() def factorial(n): if n <= 1: return 1 return n * call(factorial, n-1) print "5! =", factorial(5) print "1000! / 998! =", factorial(1000)/factorial(998) 

Output:

 5! = 120 1000! / 998! = 999000 

I have several other examples of channeling channels by channel in my presentation. This is a common thing in Stackless.

+3
source

All Articles