-
Notifications
You must be signed in to change notification settings - Fork 5
Draft: Asyncio conversion #148
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
ptsOSL
wants to merge
10
commits into
main
Choose a base branch
from
asyncio-conversion
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
10 commits
Select commit
Hold shift + click to select a range
e262e81
Switch from cothread to aioca/asyncio
ptsOSL 562e86f
More async fixes
ptsOSL 538090e
Renamed cothread_cs and removed cothread elsewhere
ptsOSL 2fa736b
Midway through updating tests
ptsOSL 39c9840
Remaining test fixes for async conversion
ptsOSL b3ee891
Switch from __bool__ to async is_enabled function
ptsOSL d1bd9a6
Check if device.set_value is async before calling
ptsOSL 33318db
Remove async where not needed and fix linting
ptsOSL e2e6046
Add aioca dependency
ptsOSL 3ccbca6
Ruff check fixes
ptsOSL 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| """Module containing pytac data source classes.""" | ||
|
|
||
| import inspect | ||
|
|
||
| import pytac | ||
| from pytac.exceptions import DataSourceException, FieldException | ||
|
|
||
|
|
@@ -189,7 +191,7 @@ def set_unitconv(self, field, uc): | |
| """ | ||
| self._uc[field] = uc | ||
|
|
||
| def get_value( | ||
| async def get_value( | ||
| self, | ||
| field: str, | ||
| handle: str = pytac.RB, | ||
|
|
@@ -225,12 +227,12 @@ def get_value( | |
| if data_source_type == pytac.DEFAULT: | ||
| data_source_type = self.default_data_source | ||
| data_source = self.get_data_source(data_source_type) | ||
| value = data_source.get_value(field, handle, throw) | ||
| value = await data_source.get_value(field, handle, throw) | ||
| return self.get_unitconv(field).convert( | ||
| value, origin=data_source.units, target=units | ||
| ) | ||
|
|
||
| def set_value( | ||
| async def set_value( | ||
| self, | ||
| field: str, | ||
| value: float, | ||
|
|
@@ -264,7 +266,7 @@ def set_value( | |
| value = self.get_unitconv(field).convert( | ||
| value, origin=units, target=data_source.units | ||
| ) | ||
| data_source.set_value(field, value, throw) | ||
| await data_source.set_value(field, value, throw) | ||
|
|
||
|
|
||
| class DeviceDataSource(DataSource): | ||
|
|
@@ -321,7 +323,7 @@ def get_fields(self): | |
| """ | ||
| return self._devices.keys() | ||
|
|
||
| def get_value(self, field, handle, throw=True): | ||
| async def get_value(self, field, handle, throw=True): | ||
| """Get the value of a readback or setpoint PV for a field from the | ||
| data_source. | ||
|
|
||
|
|
@@ -337,9 +339,17 @@ def get_value(self, field, handle, throw=True): | |
| Raises: | ||
| FieldException: if the device does not have the specified field. | ||
| """ | ||
| return self.get_device(field).get_value(handle, throw) | ||
|
|
||
| def set_value(self, field, value, throw=True): | ||
| device = self.get_device(field) | ||
| # TODO some devices dont need to be awaited as they are just retrieving stored | ||
| # data, but others get data from PVs so do, make this better | ||
| val = 0 | ||
| if inspect.iscoroutinefunction(device.get_value): | ||
| val = await device.get_value(handle, throw) | ||
| else: | ||
| val = device.get_value(handle, throw) | ||
| return val | ||
|
|
||
| async def set_value(self, field, value, throw=True): | ||
| """Set the value of a readback or setpoint PV for a field from the | ||
| data_source. | ||
|
|
||
|
|
@@ -352,4 +362,10 @@ def set_value(self, field, value, throw=True): | |
| Raises: | ||
| FieldException: if the device does not have the specified field. | ||
| """ | ||
| self.get_device(field).set_value(value, throw) | ||
| device = self.get_device(field) | ||
| # TODO some devices dont need to be awaited as they are just setting local | ||
| # data, but others set data to PVs, so do, make this better | ||
| if inspect.iscoroutinefunction(device.set_value): | ||
| await device.set_value(value, throw) | ||
| else: | ||
| device.set_value(value, throw) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs discussion on how to handle async vs non async devices.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to get an elements value, currently we must await it as it is possible that we need to do an async operation to get the data:
await element.get_value()
This then has to do a get_value on its data source manager, which again may have to do async work so must be awaited:
await data_source_manager.get_value()
This function then gets the data source which is non async, then it calls get_value on the datasource which must be awaited as the datasource could do async work:
await data_source.get_value()
Two examples of data sources are ATLatticeDataSource and SimpleDevice. Both are used by Virtac, when getting a value from an ATLatticeDataSource we must await a recalculation. When using SimpleDevice we are just getting stored data, so we dont need to do an await.
Currently these two cases are handled with: if inspect.iscoroutinefunction(device.get_value).
Maybe this could be improved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it an issue that we must await when getting a value even though it doesnt actually do any async actions? It will have a very slight computation overhead.
Most of our use cases are largely async. The main situation where we might not be is if someone loads an AT lattice and just wants to get the default values from it using pytac. They would have to await everything even though it is just reading data from the static lattice.