Skip to content

Clean up the async start_task#178

Merged
DavidMarchant merged 3 commits into
developfrom
dev/tweak-async
Nov 19, 2018
Merged

Clean up the async start_task#178
DavidMarchant merged 3 commits into
developfrom
dev/tweak-async

Conversation

@WilliamMcCumstie
Copy link
Copy Markdown
Contributor

Based on #176

This PR cleans up the start_task async method. Most of the changes are code readability changes and do not affect functionality.

There has been a change in the control logic concerning interrupt. Previously all the futures where being cancelled. This is fine for the JobTasks as this triggers them to shutdown as expected. However it also cancels the main future which is running start_tasks. This will kill the event loop prematurely and makes the callback behaviour of the other futures difficult to predict.

Instead the start_tasks will catch the CancelledError and still shutdown as per normal. This prevents additional futures from starting BUT allows currently running (/recently cancelled) futures to shutdown gracefully.

@WilliamMcCumstie
Copy link
Copy Markdown
Contributor Author

Also errors from closing the ssh connection are being caught. This should hopefully fix #177

Unfortunately the python3.5 version of `Task` doesn't have a means to
determine if a task has finished vs done. Instead the internal `_state`
method needs to be checked.

To increase the maintainability, the method for doing this has been
extracted into `finished`. This will make it easier to update in the
future if need be
Previously all tasks running in the event loop would be cancelled.
However this makes the callback behaviour unpredictable as the main
Future was also being cancelled.

The main Future (aka `start_tasks`) still gets cancelled. This stops
`add_tasks` from running. However the `await_finished` will continue
running. This ensures the other Futures finish correctly

The `CancelledError` catch has been moved to where the error is being
generated
Closing the ssh connection prematurely can lead to some ssh errors. In
an attempt to catch these, all errors from the close method are being
caught.

Also the redundant catch of `CancelledError` has been removed
Copy link
Copy Markdown
Contributor

@DavidMarchant DavidMarchant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all a little black magic-y but looks good to me!

@DavidMarchant DavidMarchant merged commit 56b07c8 into develop Nov 19, 2018
@DavidMarchant DavidMarchant deleted the dev/tweak-async branch November 19, 2018 18:19
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

Successfully merging this pull request may close these issues.

2 participants