Skip to content

Add asyncio support#359

Open
sveinse wants to merge 56 commits into
canopen-python:masterfrom
sveinse:feature-asyncio
Open

Add asyncio support#359
sveinse wants to merge 56 commits into
canopen-python:masterfrom
sveinse:feature-asyncio

Conversation

@sveinse

@sveinse sveinse commented Mar 27, 2023

Copy link
Copy Markdown
Collaborator

This PR adds support of asyncio to canopen. The overall goals is to make canopen able to be used in either with asyncio or regular synchronous mode (but not at the same time) from the same code base.

Note that this work is still work in progress. This PR was created to discuss the specific solutions for async and non-async as mentioned in #272. This PR closes #272.

Current status until feature complete:

  • Implement ABlockUploadStream, ABlockDownloadStream and ATextIOWrapper for async in SdoClient. Not needed
  • Implement EcmyConsumer.wait() for async
  • Async implementation of LssMaster Only fast_scan
  • ~Async implementation of BaseNode402~Omitted for now
  • Implement async variant of Network.add_node()
  • Update unittests for async
  • Update examples
  • Update documentation

@sveinse

sveinse commented Mar 27, 2023

Copy link
Copy Markdown
Collaborator Author

What is the best way to deal with Variable attributes? The feedback from #355 indicates that it is desirable to keep the attr based get/set mechanisms. However, this scheme cannot be used for async as there is no await mechanism built into @data.setter.

    var = param.data   # Non-async use
    var = await param.aget_data()   # Async use

My goal has been to keep the async and non-async uses of canopen as equal as possible, but this is an area where users will see a difference.

@acolomb

acolomb commented Apr 25, 2024

Copy link
Copy Markdown
Member

There is an implementation for .read() and .write() on the underlying canopen.variable.Variable type. Could that be used in an async wrapper, or something similar added for async?

@sveinse

sveinse commented Apr 25, 2024

Copy link
Copy Markdown
Collaborator Author

@acolomb I'm not precisely sure what you mean, so let me guess: You need separate async methods for read and right and they should only call their respective async setters/getters.

@acolomb

acolomb commented Apr 25, 2024

Copy link
Copy Markdown
Member

Sorry, I was trying to answer your question in the previous comment about synchronous getters / setters used in the properties. What I meant was that instead we already do have methods to access a variable remotely. Those might be a better fit to mirror to the async world:

value = sdo_var.raw  # This is the usual, terse style recommended in the docs
value = sdo_var.read(fmt='raw')  # This also works already
value = await sdo_var.aread(fmt='raw')  # Feels like a natural enhancement for async

Note that the fmt='raw' argument can of course be left out, it's only needed for phys or desc variants. This would give us an easy way to support async without changing the existing synchronous property-based access.

@sveinse sveinse marked this pull request as draft April 26, 2024 05:31
@codecov-commenter

codecov-commenter commented Feb 2, 2025

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 46.56716% with 179 lines in your changes missing coverage. Please review.

Project coverage is 69.03%. Comparing base (ae71853) to head (46f9b4a).

Files with missing lines Patch % Lines
canopen/pdo/base.py 41.07% 60 Missing and 6 partials ⚠️
canopen/nmt.py 22.72% 33 Missing and 1 partial ⚠️
canopen/variable.py 41.46% 24 Missing ⚠️
canopen/sdo/base.py 44.44% 15 Missing ⚠️
canopen/emcy.py 33.33% 12 Missing ⚠️
canopen/node/remote.py 50.00% 4 Missing and 2 partials ⚠️
canopen/sdo/client.py 45.45% 6 Missing ⚠️
canopen/network.py 76.19% 2 Missing and 3 partials ⚠️
canopen/sdo/server.py 66.66% 3 Missing and 1 partial ⚠️
canopen/async_guard.py 85.71% 1 Missing and 1 partial ⚠️
... and 3 more

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #359      +/-   ##
==========================================
- Coverage   71.36%   69.03%   -2.34%     
==========================================
  Files          26       27       +1     
  Lines        3129     3397     +268     
  Branches      480      523      +43     
==========================================
+ Hits         2233     2345     +112     
- Misses        765      906     +141     
- Partials      131      146      +15     
Files with missing lines Coverage Δ
canopen/node/base.py 100.00% <ø> (ø)
canopen/objectdictionary/__init__.py 83.33% <100.00%> (ø)
canopen/objectdictionary/eds.py 86.89% <100.00%> (+0.07%) ⬆️
canopen/profiles/p402.py 36.46% <ø> (ø)
canopen/lss.py 44.27% <90.00%> (+2.51%) ⬆️
canopen/async_guard.py 85.71% <85.71%> (ø)
canopen/sync.py 81.81% <60.00%> (-7.66%) ⬇️
canopen/timestamp.py 88.88% <60.00%> (-11.12%) ⬇️
canopen/sdo/server.py 86.95% <66.66%> (-2.02%) ⬇️
canopen/network.py 87.91% <76.19%> (-2.03%) ⬇️
... and 7 more

... and 1 file with indirect coverage changes

@codecov

codecov Bot commented May 4, 2025

Copy link
Copy Markdown

@sveinse sveinse force-pushed the feature-asyncio branch from 7486445 to 751f854 Compare May 15, 2025 15:52
sveinse added 6 commits June 12, 2025 00:56
* Make code more similar the upstream code
* Implement missing async functions
* Update README.rst and example
* Revert test cases to be diffable with upstream
* Ensure all skipTest() have useful messages
* Wash FIXMEs and NOTEs
* Adding `AllowBlocking` for temporary pausing the async guard
* skipTest() cleanup
* Increase test coverage
* OD object lookup issue
* SDO testing warning issue
* Fixed uncovered bugs
* Bumped minimum py version to 3.9 (due to asyncio compatibility)
* Added tests for PDO to increase coverage
@sveinse sveinse changed the title Add asyncio support [WIP] Add asyncio support Jun 15, 2025
@sveinse sveinse marked this pull request as ready for review June 15, 2025 13:50
@sveinse

sveinse commented Jun 15, 2025

Copy link
Copy Markdown
Collaborator Author

This branch is ready for review and integration into the project. Its not fully complete (I suppose it never will). The biggest lack is that there is no documentation for async.

The branch's README (https://github.com/sveinse/canopen-asyncio?tab=readme-ov-file#difference-between-async-and-non-async-version) contains an overview over the current differences between this branch and the main project.

The port is quite "naive" async-wise as it relies on asyncio.to_thread() on many async operations, which not ideal. The back-end of the canopen library relies on synchronous callbacks which doesn't work too well from an async perspective. To be able to support both non-async (regular) and async use, this approach have been chosen. In time I hope we can refactor the inner working of the library such that we can do it independent of the communication (e.g. vis sansio).

@acolomb acolomb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Phew, that's a lot of material and I hope you don't mind my elaborate review comments. But first, let's take a step back and see if we can better define what the PR does and what the goals are.

I'm not really experienced with asyncio based programming in Python, but I do know pretty well what an event loop and coroutines are. So my main mental challenge with this is trying to imagine what an application would really look like if using asyncio to handle its functionality elegantly. From own experience, I'd say moving long-blocking background activities from threads to a (cooperatively scheduled) task is one of the main motivations -- such as waiting for a drive homing procedure to finish.

My impression on what this focuses on, with some overall remarks about the implementation:

  1. Finding and marking all API functions potentially blocking on I/O. This is a useful thing in itself, but I don't think we want the NOTE comments in the code indefinitely. It makes sense to gather findings like you did now, but provides little extra value to the library consumer and will bit-rot really fast I suppose.

  2. Detecting improper use of blocking functions in async context, which would block the main loop. This is useful but totally optional IMHO, so could better be moved to its own PR to focus on the actual async capabilities first. An approach with a single assert canopen.async_guard.ensure_not_async line (or similar) could be just as good as the decorator approach, but clearer.

  3. Arranging user callbacks to be scheduled in the event loop instead of called immediately. I see several alternatives to the current dispatcher solution, so let's clarify what situation this applies to: A CAN message arrives on the network and the application needs to passively react to this external trigger. The reaction may be an internal callback (as in p402.py) or an arbitrarily long-running external application function. This uncertainty must be contained to not block the main event loop. Traditionally, it is executed inside the RX thread of python-can's Notifier anyway, blocking that at worst. There is already functionality in there to schedule callbacks as tasks if they are coroutines, execute directly otherwise. So the easiest would be for the application to provide a coroutine directly as callback -- this just needs to be passed through the canopen library, so that the listener is also an async / await wrapper around the application callback. But shouldn't it be an application responsibility to decide whether the callback happens inside the RX background thread, or as a task on the event loop?

  4. Provide async (coroutine) variants of common blocking operation methods. This is the core of the "async integration" API-wise. But it's actually much less of a concern than the callback handling. Because the caller can always just do as you did in many places, calling await asyncio.to_thread() on the existing synchronous function. Some of the really interesting functions are not converted to async variants yet. I suppose that to_thread() approach is a stop-gap solution before implementing real, awaitable internal methods.

  5. Add unit tests for the new API. This is probably the largest portion of the diff, but I haven't checked it thoroughly as these high-level review points must be discussed and solved first.

Sorry if I missed something now, or it's just my lack of understanding asyncio in general. Plus I get too tired when tackling such a complex and large review late at night. But that's already lots of food for discussion I suppose.

Comment thread canopen/network.py
NOTIFIER_SHUTDOWN_TIMEOUT: float = 5.0 #: Maximum waiting time to stop notifiers.

def __init__(self, bus: Optional[can.BusABC] = None):
# NOTE: Function arguments changed to provide notifier, see #556

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As discussed in #556, this can simply be set between __init__() and connect(), with the latter only creating one if not already set (as you did).

Comment thread canopen/network.py
Comment on lines +42 to +43
self.bus: Optional[can.BusABC] = bus
self.loop: Optional[asyncio.AbstractEventLoop] = loop

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The type hints here are inferred from the arguments, better skip repeating them.

Comment thread canopen/network.py
Comment on lines +62 to +65
# Register this function as the means to check if canopen is run in
# async mode. This enables the @ensure_not_async() decorator to
# work. See async_guard.py
set_async_sentinel(self.is_async())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not "registering a function", but simply flipping a switch concerning the current thread ID, right? The comment should be adapted, maybe leftover from a previous design?

Comment thread canopen/network.py
logger.error("An error has caused receiving of messages to stop")
raise exc

def is_async(self) -> bool:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be a @property.

Comment thread canopen/network.py
# Exceptions in any callbaks should not affect CAN processing
logger.exception("Exception in callback: %s", exc_info=exc)

def dispatch_callbacks(self, callbacks: List[Callback], *args) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is one of the central elements it seems, making it easy to influence what exactly "invoking a callback" means and does. The approach is alright and the Network seems like a good place to handle such a definition centrally.

However, I'm unsure whether this is the best / only solution regarding callbacks. One alternative that springs to mind is to wrap each given callback function when it is registered somewhere, replacing it with a callable that creates the corresponding task instead of executing the given callback directly.

Comment thread README.rst
Comment on lines +67 to +69
* The mechanism for CAN bus callbacks have been changed. Callbacks might be
async, which means they cannot be called immediately. This affects how
error handling is done in the library.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This sounds a lot like duplicating what python-can already provides. Can we embrace that lower-level concept more instead of building our own dispatcher?

Comment thread README.rst
* :code:`EmcyConsumer.on_emcy`
* :code:`NtmMaster.on_heartbaet`

* SDO block upload and download is not yet supported in async mode.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Which would be a really good fit for a coroutine I suppose, if done right :-)

Comment thread README.rst
* :code:`ODVariable.__len__()` returns 64 bits instead of 8 bits to support
truncated 24-bits integers, see #436

* :code:`BaseNode402` does not work with async

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can imagine how a homing procedure for example would benefit from being called with await.

Comment thread canopen/sdo/server.py
"""
return self._node.get_data(index, subindex)

async def aupload(self, index: int, subindex: int) -> bytes:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since there is no blocking access, we don't really need the async method variants on the SdoServer (nor SdoBase for that matter). Unless LocalNode.get_data() is also allowed to be a blocking access pattern, implemented as a coroutine itself, there is no value in wrapping it here like this. The only expected blocking I/O on the library side is the SdoClient, thus the async methods can be introduced only there.

Comment thread canopen/pdo/base.py
Comment on lines +352 to +353
def read_generator(self):
"""Generator to run through steps for reading the PDO configuration

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this solution. Maybe a similar pattern can be applied in other places?

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.

Add async support to canopen

3 participants