在 stackless Python 中,你可以通过通道发送通道吗?

发布于 2024-07-15 03:07:47 字数 478 浏览 16 评论 0原文

我当前没有运行 stackless,因此我无法自己尝试。

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

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

ch2ch3 是同一个频道吗? 说:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-07-22 03:07:47

是的。 刚刚测试过。

>>> 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

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
追风人 2024-07-22 03:07:47

通道发送普通的 Python 引用,因此您发送的数据(通道、字符串等)正是接收到的数据。

通过通道发送通道的一个示例是当您使用微线程作为服务时,即微线程在通道上侦听请求、执行工作并返回结果。 请求中需要包含工作的数据和结果的返回通道,以便结果到达请求者。

这是我几年前在 PyCon 上为 Stackless 演讲开发的一个极端示例。 这会为每个函数调用创建一个新的 tasklet,因此我可以使用阶乘的递归实现,而无需担心 Python 的堆栈限制。 我为每个调用分配一个微线程,它获取结果的返回通道。

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)

输出是:

5! = 120 
1000! / 998! = 999000

在我的演示文稿中,我还有一些通过通道发送通道的其他示例。 这在 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.

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)

The output is:

5! = 120 
1000! / 998! = 999000

I have a few other examples of sending channels over channels in my presentation. It's a common thing in Stackless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文