Your PLC Doesn't Care About Your Event Loop
Four lessons from putting asyncio on the factory floor At some point, almost every automation team picks up Python. It rarely starts as a big decision. Someone needs a quick script to log a few PLC variables to a CSV. Then the script needs a small web dashboard. Then it has to push data to a database, watch a second machine, and send an alert when a value drifts. A few months later the "quick…
Four key takeaways emerged from moving asyncio to the factory floor:
1. Round-trips are costly. A PLC responds quickly to a single read, but repeated requests cause latency. Batch requests together using sum commands to reduce round-trip time. However, remember that the PLC limits how much data can be batched in one request.
2. Async does not equal non-blocking. While the network side of an async ADS client is straightforward, decoding large responses is CPU-intensive and can block the event loop if not handled properly. Split work between chunks and use a thread pool for decoding to keep the event loop responsive.
3. Polling is a habit, but notifications are better. ADS offers a push-based model where the PLC sends updates when values change. Implement bounded queues with explicit overflow policies to handle cases where the consumer cannot keep up with the PLC's data rate. Tailor the backpressure policy to your specific use case (e.g., drop oldest, keep recent values, or deliver only the current value).
4. Keep protocol and transport separate. While the classic setup is a client connecting directly to a controller via TCP, modern installations often involve edge gateways, firewalls, or message brokers like MQTT. Design your code with a clear separation between the protocol (ADS) and the transport (TCP or MQTT) to make future network changes less painful.
Rewritten by urgent.news; may contain errors — check the original. How we use AI.
Source: dev.to