-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make generator state easier to introspect #54429
Comments
Generators can be in four different states that may be relevant to framework code making use of them (especially as coroutines). This state is all currently available from Python code, but is rather obscure and could be made readable. The four states are: Created: Currently executing: Suspended at a yield point: Exhausted/closed: My API proposal is to add the following to the inspect module: GEN_CREATED, GEN_RUNNING, GEN_SUSPENDED, GEN_CLOSED = range(4) def getgeneratorstate(g):
if g.gi_running:
return GEN_RUNNING
if g.gi_frame is None:
return GEN_CLOSED
if g.gi_frame.f_lasti == -1:
return GEN_CREATED
return GEN_SUSPENDED (the lack of underscores in the function name follows the general style of the inspect module) |
Is it CPython-specific? |
On Thu, Oct 28, 2010 at 10:55 PM, Antoine Pitrou <report@bugs.python.org> wrote:
The states are not CPython-specific (they're logical states of the
"Currently executing" means the frame is being evaluated in a Python |
I could imagine separating the state into two parts:
The latter is just g.gi_running so we don't need a new API for this. :-) |
So something like: GEN_CREATED, GEN_ACTIVE, GEN_CLOSED = range(3) def getgeneratorstate(g):
"""Get current state of a generator-iterator.
Possible states are:
GEN_CREATED: Created, waiting to start execution
GEN_ACTIVE: Currently being executed (or suspended at yield)
GEN_CLOSED: Execution has completed Use g.gi_running to determine if an active generator is running or Having 4 separate states actually makes the documentation a little easier to write: def getgeneratorstate(g):
"""Get current state of a generator-iterator.
Possible states are:
GEN_CREATED: Waiting to start execution
GEN_RUNNING: Currently being executed by the interpreter
GEN_SUSPENDED: Currently suspended at a yield expression
GEN_CLOSED: Execution has completed
""" Checking if the generator is active is then just a matter of checking "gen_state in (GEN_RUNNING, GEN_SUSPENDED)". |
I take it back. The 4-value state looks better. My initial hesitance was that if you ever see GEN_RUNNING you are |
Indeed, the minimal lifecycles are: GEN_CREATED->GEN_CLOSED (exception thrown in before the generator was even started) Other cases following the same basic pattern as the last one, they just bounce back and forth between suspended and running more times. It occurred to me that threads really use the same state machine, it's just that almost nobody writes their own Python thread schedulers, so only _thread and threading care about the suspended/running distinction. There are quite a few different generator schedulers though, so the distinctions matters to more 3rd party code than it does for threads. |
This patch adds the getgeneratorstate function and related tests. |
Looks good, modulo two nitpicks:
|
Done, thank you! Second version attached. |
Nice. Now you can sit back, relax and wait for Nick to commit the patch or make further comments. |
I'll actually go with version 1 of the patch as far as the variable initialisation goes. Yes, it is fractionally slower, but you get a maintenance gain from the fact that the enum values are guaranteed to be orthogonal, and this is clearly obvious to the reader. When you write the assignments out explicitly, the reader has to actually look at the assigned values to determine whether or not the same value is ever assigned twice. (No need to post a modified patch - I'll fix it before I check it in) |
Committed in r86633. I added the missing docs changes, and tweaked the tests a little bit:
|
Temporarily reopening to remind me to switch from using integer constants to strings (which are much friendlier for debugging purposes). |
Yes please. On Mon, Nov 22, 2010 at 7:44 AM, Nick Coghlan <report@bugs.python.org> wrote:
|
Switched to strings in r86879. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: