-
Notifications
You must be signed in to change notification settings - Fork 6
Simplify event loop handling and remove deprecated get_event_loop #340
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
Open
GDYendell
wants to merge
2
commits into
main
Choose a base branch
from
simplify-event-loop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # The FastCS class | ||
|
|
||
| `FastCS` is the entrypoint for a fastcs application. It connects a `Controller` to one | ||
| or more `Transport`s, runs the controller's update loops as background tasks, and | ||
| manages the full application lifecycle from startup to shutdown. | ||
|
|
||
| ## Construction | ||
|
|
||
| ```python | ||
| from fastcs import FastCS | ||
| from fastcs.controllers import Controller | ||
| from fastcs.transports.epics import EpicsCATransport | ||
|
|
||
|
|
||
| class MyController(Controller): | ||
| pass | ||
|
|
||
|
|
||
| control_system = FastCS(controller=MyController(), transports=[EpicsCATransport()]) | ||
|
|
||
| control_system.run() | ||
| ``` | ||
|
|
||
| ## Startup and Runtime | ||
|
|
||
| Calling `control_system.run()` (or `await control_system.serve()`) executes the | ||
| following steps in order: | ||
|
|
||
| 1. **Initialise the controller:** `controller.initialise()` is awaited, allowing the | ||
| controller to query the device and dynamically add attributes before the API is | ||
| built. After that, `controller.post_initialise()` is called to perform any final | ||
| setup, such as validating all type hints are satisfied. | ||
|
|
||
| 2. **Build the API:** `controller.create_api_and_tasks()` returns the `ControllerAPI` | ||
| that transports will use, plus two lists of coroutines: *initial tasks* (run once at | ||
| startup) and *scan tasks* (run as continuous background loops). | ||
|
|
||
| 3. **Connect transports:** each transport's `connect()` method is called with the | ||
| `ControllerAPI`. This lets the transport inspect the controller's attributes and | ||
| commands to prepare its protocol-specific representations before serving begins. | ||
|
|
||
| 4. **Connect the controller:** `controller.connect()` is called to open the connection | ||
| to the device and perform any other setup logic. | ||
|
|
||
| 5. **Run initial tasks:** each initial-task coroutine is awaited in sequence. These are | ||
| `@scan(period=ONCE)` methods and `AttributeIO` update callbacks with | ||
| `update_period=ONCE`. | ||
|
|
||
| 6. **Start scan tasks:** each scan task coroutine is wrapped in an `asyncio.Task` and | ||
| run as a background task for the lifetime of the application. | ||
|
|
||
| 7. **Gather transport coroutines:** `asyncio.gather` runs all transport `serve()` | ||
| coroutines concurrently. Each transport begins accepting and responding to protocol | ||
| requests. | ||
|
|
||
| 8. **Scan pause and reconnect**: If any scan tasks raise exceptions, all scan tasks are | ||
| paused until `reconnect`. | ||
|
|
||
| ## The Interactive Shell | ||
|
|
||
| Alongside the transport coroutines, FastCS launches an embedded | ||
| [IPython](https://ipython.org/) shell (unless `interactive=False` is passed). The shell | ||
| namespace is pre-populated with: | ||
|
|
||
| | Name | Value | | ||
| |------|-------| | ||
| | `controller` | The root controller instance | | ||
| | `transports` | The class names of active transports | | ||
| | `run` | A helper that schedules a coroutine on the FastCS event loop from the IPython thread | | ||
| | *transport-specific keys* | Any entries exposed via each transport's `context` property | | ||
|
|
||
| The shell runs in a separate thread so it does not block the asyncio event loop. When | ||
| the user exits the shell, the application begins its shutdown sequence. | ||
|
|
||
| When `interactive=False` a simple coroutine that blocks forever keeps the application | ||
| alive until the task is cancelled externally (e.g. SIGINT). | ||
|
|
||
| ## Shutdown Sequence | ||
|
|
||
| When then application is stopped, FastCS performs an orderly teardown: | ||
|
|
||
| 1. **Cancel scan tasks:** each background scan task is cancelled and removed, stopping | ||
| all periodic polling. | ||
|
|
||
| 2. **Disconnect the controller:** `controller.disconnect()` is awaited, allowing the | ||
| controller to release device resources cleanly. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.