使用asyncua模块的call_method方法调用OPC UA的Server端方法报错:asyncio.exceptions.TimeoutError
报错信息如下:
Traceback (most recent call last):
asyncio.run(main())
File “D:\miniconda3\envs\py31013\lib\asyncio\runners.py”, line 44, in run
return loop.run_until_complete(main)
File “D:\miniconda3\envs\py31013\lib\asyncio\base_events.py”, line 649, in run_until_complete
return future.result()
File “”, line 60, in main
async with Client(url=url) as client:
File “D:\miniconda3\envs\py31013\lib\site-packages\asyncua\client\client.py”, line 98, in aexit
await self.disconnect()
File “D:\miniconda3\envs\py31013\lib\site-packages\asyncua\client\client.py”, line 348, in disconnect
await self.close_session()
File “D:\miniconda3\envs\py31013\lib\site-packages\asyncua\client\client.py”, line 703, in close_session
return await self.uaclient.close_session(True)
File “D:\miniconda3\envs\py31013\lib\site-packages\asyncua\client\ua_client.py”, line 366, in close_session
data = await self.protocol.send_request(request)
File “D:\miniconda3\envs\py31013\lib\site-packages\asyncua\client\ua_client.py”, line 167, in send_request
data = await asyncio.wait_for(self._send_request(request, timeout, message_type), timeout if timeout else None)
File “D:\miniconda3\envs\py31013\lib\asyncio\tasks.py”, line 458, in wait_for
raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
解决asyncio.exceptions.TimeoutError的方法如下:
OPC UA Client python asyncua error when connecting and disconnecting
这篇文章中说明了报错:asyncio.exceptions.TimeoutError的原因,是由于call_method调用的方法执行时间太长了,但是Client端同步执行的检查服务是否处于在线状态的背景任务执行超时了,最终导致抛出报错:asyncio.exceptions.TimeoutError,拿不到Server执行调用方法后的结果。
解决措施是在开启Client会话的函数里面增加timeout超时时间,并且将timeout设置大于Client调用Server端方法的执行时间,将timeout设置成一个比较大的数。