【问题标题】:In stackless Python, can you send a channel over a channel?在 stackless Python 中,您可以通过通道发送通道吗?
【发布时间】:2009-03-12 12:46:57
【问题描述】:

我目前没有运行 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()

这个功能让我想起了罗伯特·派克(Plan9 成名)在 Google 提供的 talk about Newsqueak。在 Newsqueak 中,您可以通过频道发送频道。

【问题讨论】:

  • 有趣的是,这会出现,我在想 Stackless 的确切内容,以及我多么喜欢它拥有像 Go/Newsqueak 这样的频道。

标签: python stackless python-stackless


【解决方案1】:

是的。刚刚测试。

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

【讨论】:

    【解决方案2】:

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

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

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

    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 中很常见。

    【讨论】:

      猜你喜欢
      • 2015-05-20
      • 2011-10-01
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多