문제

가 없 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()

이 기능을 나 에 대해 이야기 Newsqueak 로버트 파이크(의 Plan9 명)준 Google.에 Newsqueak 을 보낼 수있는 채널을 통한 채널입니다.

도움이 되었습니까?

해결책

예. 방금 테스트했습니다.

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

다른 팁

채널를 보내는 파이썬 참조 데이터 보내기(채널,string,무엇이든)는 정확히 무엇인가 받았습니다.

하나의 예를 보내는 채널을 통해서 채널을 사용할 때 tasklet 서비스로서,즉,tasklet 수신하는 채널에 대한 요청이 작동하지 않은 결과를 반환합니다.요청에 필요 데이터를 포함하는 작업에 대한 반환을 위한 채널의 결과,그래서 그는 결과가 요청자.

여기에서 극단적인 예를 들어 내가 개발한 Stackless 에서 이야기 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top