[cogen] Yield inside helper function that is called from a coro

7 views
Skip to first unread message

TK

unread,
May 4, 2010, 6:05:19 PM5/4/10
to cogen
Pleas tell me, why doesn't the following work? The code within
_processData() does not yield anything to add data to my
cogen.core.queue.Queue object. And there are no error messages
whatsoever.

Interestingly, I can return that messageQueue back to
processMessage(), and then yield from there. That seems to work as
expected.

I don't understand why I can't put a yield inside a helper function.
It will help clean up my code.

def _processData(self, element):
messageQueue = someDict['somekey']
yield messageQueue.put(message)

@coro
def processMessage(self, **kwargs):
pubsub = kwargs['pubsub']
yield pubsub.subscribe()

while 1:
result = (yield pubsub.fetch())
for element in result:
self._processData(element)


Thanks !

--
You received this message because you are subscribed to the Google Groups "cogen" group.
To post to this group, send email to co...@googlegroups.com.
To unsubscribe from this group, send email to cogen+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cogen?hl=en.

Ionel Maries Cristian

unread,
May 5, 2010, 12:40:36 PM5/5/10
to co...@googlegroups.com
Well, essentially a coroutine is really a generator so you can't yield from a called function in the generator. This a limitation of the generators.

A possible solution for your case can be (changes in bold):

   @coro

   def _processData(self, element):
       messageQueue = someDict['somekey']
       yield messageQueue.put(message)

   @coro
   def processMessage(self, **kwargs):
       pubsub = kwargs['pubsub']
       yield pubsub.subscribe()

       while 1:
           result = (yield pubsub.fetch())
           for element in result:
               yield self._processData(element)

-- ionel

TK

unread,
May 5, 2010, 1:18:02 PM5/5/10
to cogen
I suspected something like this. That's not really a problem. After
all, there will be very little extra overhead, as far as I can tell.

Thanks again for the swift response.

Regards,

On May 5, 6:40 pm, Ionel Maries Cristian <ionel...@gmail.com> wrote:
> Well, essentially a coroutine is really a generator so you can't yield from
> a called function in the generator. This a limitation of the generators.
>
> A possible solution for your case can be (changes in bold):
>
>    *@coro*
>    def _processData(self, element):
>        messageQueue = someDict['somekey']
>        yield messageQueue.put(message)
>
>    @coro
>    def processMessage(self, **kwargs):
>        pubsub = kwargs['pubsub']
>        yield pubsub.subscribe()
>
>        while 1:
>            result = (yield pubsub.fetch())
>            for element in result:
>               * yield self._processData(element)*
> > cogen+un...@googlegroups.com <cogen%2Bunsu...@googlegroups.com>.
> > For more options, visit this group at
> >http://groups.google.com/group/cogen?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "cogen" group.
> To post to this group, send email to co...@googlegroups.com.
> To unsubscribe from this group, send email to cogen+un...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/cogen?hl=en.
Reply all
Reply to author
Forward
0 new messages