why greenlet, gevent or the stackless are needed?

S

self.python

(I'm very new to this coroutine part
so It's not supposed to attack these modules,
just I don't know the differences)

atfer version 2.5, python officially support coroutine with yield.
and then, why greenlet, gevent, Stackless python are still useful?

it there somthing that "yield" can't do
or just it is easier or powerful?
 
D

Devin Jeanpierre

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
 
S

self.python

r

2012ë…„ 7ì›” 7ì¼ í† ìš”ì¼ ì˜¤í›„ 4ì‹œ 33분 26ì´ˆ UTC+9, Devin Jeanpierre ë‹˜ì˜ ë§:
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?
 
S

self.python

r

2012ë…„ 7ì›” 7ì¼ í† ìš”ì¼ ì˜¤í›„ 4ì‹œ 33분 26ì´ˆ UTC+9, Devin Jeanpierre ë‹˜ì˜ ë§:
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?
 
D

Damjan

(I'm very new to this coroutine part
so It's not supposed to attack these modules,
just I don't know the differences)

atfer version 2.5, python officially support coroutine with yield.
and then, why greenlet, gevent, Stackless python are still useful?

it there somthing that "yield" can't do
or just it is easier or powerful?

The greenlet site has some very simple examples what it can provide.
For example jumping from one function in another, and back

http://greenlet.readthedocs.org/en/latest/index.html


Gevent then uses greenlet to do lightweight "processes" (greenlets) that
are I/O scheduled. This allows for a simple model of programming that
scales to a large number of concurrent connections. You could do that
with threads but you can't start as many threads as greenlets, since
they have a much larger memory address space footprint.

There's one function, called the gevent hub, that waits for any I/O
event and then switches to the function that "blocked" on that I/O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top