Skip to content
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 it easier to see if a Task is created/running/exited #3079

Open
jakkdl opened this issue Sep 2, 2024 · 4 comments
Open

Make it easier to see if a Task is created/running/exited #3079

jakkdl opened this issue Sep 2, 2024 · 4 comments

Comments

@jakkdl
Copy link
Member

jakkdl commented Sep 2, 2024

It is currently possible to check the status of a task with https://docs.python.org/3/library/inspect.html#inspect.getcoroutinestate, but this is fairly obscure and not very readable.
We should either document this, or add properties to Task that are easy to interface with.
For implementing it, I think has_started() -> bool and has_exited -> bool would be a clean way of doing it.

I personally encountered this in #3035 (comment) which would only be for internal use, but I'm guessing it might be useful enough to surface in the public API.

See discussion in Gitter: https://matrix.to/#/!OqDVTrmPstKzivLwZW:gitter.im/$umF5oOw9d2Z20gb95GF_C48HyBi2l6eD3ZW4r_4ark0?via=gitter.im&via=matrix.org&via=zoltan.site

@mikenerone
Copy link
Member

I don't know the performance characteristics of getcoroutinestate(), but it might be faster to instead just set boolean attributes on the task object when the transition events actually happen.

@A5rocks
Copy link
Contributor

A5rocks commented Sep 2, 2024

This doesn't need to be the fastest and getcoroutinestate() seems to check booleans stored on the coroutine.

I would prefer an enum over two methods because otherwise we can represent states that are impossible (a task that exited but hasn't started?).


In addition, defined correctly, an enum allows for simple comparison:

>>> import enum
>>> class LifecycleEnum(enum.Enum):
...   A = enum.auto()
...   B = enum.auto()
...   C = enum.auto()
...
>>> LifecycleEnum.A.value < LifecycleEnum.B.value
True

(We can use specific numbers rather than enum.auto() to make sure we can add states if we want, later. But probably a bad idea.)

@jakkdl
Copy link
Member Author

jakkdl commented Sep 3, 2024

In general I find enums to be much more cumbersome to work with, mytask.has_exited vs mytask.run_status == trio.TaskRunStatus.HAS_EXITED, and if a user decides to write not mytask.has_started and mytask.exited then it's a pretty egregious logic error. And if you really do want complete enumeration you can directly use inspect.getcoroutinestate (which we'll definitely mention in the docstring). This argument is much weaker if we'd end up implementing this as more than just a thin wrapper around inspect.getcoroutinestate though.

@TeamSpen210
Copy link
Contributor

In some of my own code, I've used enums with propertys defined on them to encapsulate comparisons, nicely compact. The main concern is if we need to add another state later, people doing direct comparisons wouldn't handle that new state, while a bunch of properties would keep the code working still.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants