why greenlet, gevent or the stackless are needed?

self.python howmuchistoday at gmail.com
Sat Jul 7 10:29:11 CEST 2012


r

2012년 7월 7일 토요일 오후 4시 33분 26초 UTC+9, Devin Jeanpierre 님의 말:
> On Sat, Jul 7, 2012 at 3:09 AM, self.python <howmuchistoday at gmail.com> wrote:
> > it there somthing that "yield" can't do
> > or just it is easier or powerful?
> 
> couroutine-like generators can't give up control flow unless they are
> the top level function handled by the coroutine controller thing. For
> example, we can do this:
> 
>   def foo():
>       while True:
>           next_value = (yield)
>           print next_value
> 
> But we can't do this:
> 
>   def yap():
>       next_value = (yield)
>       print next_value
> 
>   def foo():
>       while True:
>           yap()
> 
> If we explicitly say that "yap" can control us, via "yield from" (new
> in Python 3.3), then we can do something like the above, but this
> still requires explicit markup. In all other releases of Python, this
> is impossible.
> 
> On the other hand, coroutines in greenlet et al can do a coroutine
> context switch at any point. The upside is that this is more flexible
> (and does something generators pre-3.3 cannot). The downside is that
> you now need locking structures to guarantee atomic interactions with
> a shared resource, whereas with generators you know that you always
> are the sole thing running, until you do a yield (and unless real
> threads or greenlet or whatever are involved, of course.)
> 
> -- Devin

first, thanks for good answer:)
but I don't understand why the code



  def yap(): 
      next_value = (yield) 
      print next_value 

  def foo(): 
      while True: 
          yap() 

really do.
what is the purpose of that code?



More information about the Python-list mailing list