在 stackless Python 中,你可以通过通道发送通道吗?
我当前没有运行 stackless,因此我无法自己尝试。
import stackless
ch1 = stackless.channel()
ch2 = stackless.channel()
ch1.send(ch2)
ch3 = ch1.receive()
ch2 和 ch3 是同一个频道吗? 说:
text = "Hallo"
ch2.send(text)
assert text == ch3.receive()
这个功能让我想起 Robert Pike( 的href="http://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs" rel="nofollow noreferrer">Plan9 为 Google 带来了名气。 在 Newsqueak 中,您可以通过频道发送频道。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。 刚刚测试过。
Yes. Just tested.
通道发送普通的 Python 引用,因此您发送的数据(通道、字符串等)正是接收到的数据。
通过通道发送通道的一个示例是当您使用微线程作为服务时,即微线程在通道上侦听请求、执行工作并返回结果。 请求中需要包含工作的数据和结果的返回通道,以便结果到达请求者。
这是我几年前在 PyCon 上为 Stackless 演讲开发的一个极端示例。 这会为每个函数调用创建一个新的 tasklet,因此我可以使用阶乘的递归实现,而无需担心 Python 的堆栈限制。 我为每个调用分配一个微线程,它获取结果的返回通道。
输出是:
在我的演示文稿中,我还有一些通过通道发送通道的其他示例。 这在 Stackless 中是很常见的事情。
Channels send normal Python references so the data you send (channel, string, whatever) is exactly what is received.
One example of sending a channel over a channel is when you use a tasklet as a service, that is, a tasklet listens on a channel for requests, does work, and returns the result. The request needs to include the data for the work and the return channel for the result, so that the result goes to the requestor.
Here's an extreme example I developed for my Stackless talk at PyCon a few years ago. This creates a new tasklet for each function call so I can use a recursive implementation of factorial which doesn't need to worry about Python's stack limit. I allocate a tasklet for each call and it gets the return channel for the result.
The output is:
I have a few other examples of sending channels over channels in my presentation. It's a common thing in Stackless.