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

Add named tasks #2304

Merged
merged 13 commits into from Dec 20, 2021
Merged

Add named tasks #2304

merged 13 commits into from Dec 20, 2021

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Nov 7, 2021

This is a fairly simple PR that adds something that adds naming asyncio.Task. If you create a named task, it will also allow you to add it to a registry (if requested) for simple callback after.

app.add_task(dummy, name="dummy_task")
task = app.get_task("dummy_task")

This PR adds the following new attributes to application instances:

  • app.get_task
  • app.cancel_task
  • app.purge_tasks
  • app.shutdown_tasks
  • app.tasks

In addition, on app.add_task it has a return value of the new task IF you are inside of the running loop.

TODO:

  • Simple API to cleanup the registry from done tasks
  • Some more tests around task registration
  • Potentially an easy callable to cancel tasks: app.cancel_task("foo")
  • Potentially also providing the task as a return value on add_task when created inside application (and not in global scope where it is added to delayed execution)
  • Solve app.stop() does not run tasks to completion #2267

Closes #2301
Closes #2267

@codeclimate
Copy link

codeclimate bot commented Nov 7, 2021

Code Climate has analyzed commit ebc4a7a and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (86% is the threshold).

This pull request will bring the total coverage in the repository to 86.8% (0.0% change).

View more on Code Climate.

@ahopkins ahopkins marked this pull request as ready for review December 13, 2021 12:50
@ahopkins ahopkins requested a review from a team as a code owner December 13, 2021 12:50
@ahopkins ahopkins added the needs review the PR appears ready but requires a review label Dec 13, 2021
@ahopkins ahopkins merged commit d799c5f into main Dec 20, 2021
@ahopkins ahopkins deleted the task-registry branch December 20, 2021 21:50
@ahopkins
Copy link
Member Author

This pull request has been mentioned on Sanic Community Discussion. There might be relevant details there:

https://community.sanicframework.org/t/add-task-that-contains-an-infinite-loop-will-cause-an-error-when-stopping-the-server/948/4

ChihweiLHBird pushed a commit to ChihweiLHBird/sanic that referenced this pull request Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review the PR appears ready but requires a review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Task naming app.stop() does not run tasks to completion
2 participants