generator - Understand the producer and receiver using coroutine in python -
i want use coroutine implement producer , receiver. idea using 2 coroutines , 1 producer , 1 recevier. understand coroutine's send
, running mode wrong. here code :
def coroutine(func): def start(*args,**kwargs): cr = func(*args,**kwargs) cr.next() return cr return start class producer(object): def __init__(self, recevier): self.count=1 self.producer_coroutine = self._producer() self.receiver = receiver @coroutine def _producer(self): print "waiting" yield while true: self.send("yeah, no, yeah, no") get_feedback = (yield ) print ("get feedback %s"%get_feedback) self.send("a series of tubes") break def send(self,arg): self.receiver.receive_coroutine.send(arg) def init_producer(self): self.producer_coroutine.send("begin send , receive") class recevier(object): def __init__(self): self.count=1 self.receive_coroutine=self._rececive() self.producer = none def setting_producer(self, producer): self.producer = producer @coroutine def _rececive(self): while true: line = (yield) print("get line : %s" %line) self.feedback("got it") def feedback(self, arg): self.producer.producer_coroutine.send(arg) receiver = recevier() producer = producer(receiver) receiver.setting_producer(producer) producer.init_producer()
python give me error:
waiting line : yeah, no, yeah, no traceback (most recent call last): file "test/test_coroutine.py", line 56, in <module> producer.init_producer() file "test/test_coroutine.py", line 31, in init_producer self.producer_coroutine.send("begin send , receive") file "test/test_coroutine.py", line 21, in _producer self.send("yeah, no, yeah, no") file "test/test_coroutine.py", line 28, in send self.receiver.receive_coroutine.send(arg) file "test/test_coroutine.py", line 47, in _rececive self.feedback("got it") file "test/test_coroutine.py", line 50, in feedback self.producer.producer_coroutine.send(arg) valueerror: generator executing
update: find greenlet may implement communication. :
from greenlet import greenlet def test1(): global print 12, "begin switch" gr2.switch() = 4 print " begin switch too" gr2.switch() def test2(): global print 56,"come test1, swich" gr1.switch() print print 78 a=5 gr1 = greenlet(test1) gr2 = greenlet(test2) gr1.switch()
when call producer.init_producer()
, following happens:
_producer
sends"yeah, no, yeah, no"
._rececive
receives"yeah, no, yeah, no"
, prints it._rececive
callsself.feedback
.self.feedback
sends"got it"
_producer
- however,
_producer
still @ lineself.send("yeah, no, yeah, no")
, because_rececive
has notyield
ed yet. - as
_producer
not onyield
statement, cannot receive send, , exception thrown.
therefore, problem feedback loop. i'm not sure why feedback loop needed, if comment out send_feedback
, get_feedback
lines, program works fine, producing:
waiting line : yeah, no, yeah, no line : series of tubes traceback (most recent call last): file "d.py", line 58, in <module> producer.init_producer() file "d.py", line 32, in init_producer self.producer_coroutine.send("begin send , receive") stopiteration
Comments
Post a Comment