[Tutor] looping generator

richard kappler richkappler at gmail.com
Thu Jan 7 13:31:15 EST 2016


Alan, have you ever actually been guilty of 'missing something'? :-) Not
that I've seen in my years on the list! We went through pretty much the
same thought process you put in your message in disecting our original code
and came up with something that works, which I sent under separate cover at
about the same time you sent this. I hope you will give us a critique on it.

regards, Richard

On Thu, Jan 7, 2016 at 1:16 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 07/01/16 17:02, richard kappler wrote:
>
> > def dataRecv(connection):
> >     print 'receiving'
> >     while True:
> >         data = connection.recv(65536)
> >         while True:
> >             print "writing to data.in"
> >             f2.write(data)
> >             start = data.find('\x02')
> >             end = data.find('\x03')
> >             message = data[start+1:end]
> >             print "writing to messages.out"
> >             f3.write(message)
> >             yield message
>
> There is something a bit weird going on here.
> You are using yield to exit the function.
> But the yield is inside an infinite loop with
> no other break mechanism.
> But that infinite loop is inside another infinite loop.
> I don't see how you can ever complete the inner loop
> so that it goes back to the outer loop.
>
> Remember that when you use yield the function freezes.
> Next time you call the function (in your outermost
> while True loop) the function resumes just after the
> yield - which is your innermost while loop.
> How does it ever get back to the outer loop in
> your function to read more data?
>
> I think you need to rethink, and probably simplify,
> the logic.
>
> Or maybe I'm just missing something...
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 

All internal models of the world are approximate. ~ Sebastian Thrun


More information about the Tutor mailing list