AndreasHeine / opcua-sub-to-websocket

OPC UA Server -> OPC UA Client / WebSocket Server -> WebSocket Client
12 stars 5 forks source link

Wrong results in websocket interface at datachange. #2

Open gailjkm opened 12 months ago

gailjkm commented 12 months ago

Just discovered an error in the websocket interface. Some values are sent doubled, others are missing.

After investigating I found the root cause. The queue gets modified inside the for loop. This is a fault and leads to unexpected behaviour.

https://github.com/AndreasHeine/opcua-sub-to-websocket/blob/b272dca8c8abf371e5e35f05ce93949bdb775723/client.py#L195-L206

The solution is to not pop the single items, but clear the list in once after the loop handling. Or as an more complex alternative use the Queue class.

The same is valid for the event loop.

AndreasHeine commented 12 months ago

your right!

i might rewrite it like here: https://github.com/AndreasHeine/opcua-sub-to-mqtt/blob/master/opcua_client_mqtt_publisher.py

another way would be an async generator instead of a Queue!?

gailjkm commented 12 months ago

I am not an expert in async function calls.

For me the proposed solution works. Instead of poping the single elements clear the list at once. (This is even sligthly more perfomant than modifing the list one by one. As the list is locked during the whole loop, there is no disadvantge in this.) event_notification_queue.clear()

If you see advantages in async queue then feel free to implement this solution.

duduyoyo commented 1 month ago

Alternatively, if your OPC vendor happens to provide classic OPC server(DA/AE/HDA) as well, you can try this solution. Comparing with UA this solution is much more simple, intuitive and straightforward. It works in network transport level so there is no any 3rd OPC library required in Python environment. Just FYI, cheers!