FreeOpcUa / opcua-asyncio

OPC UA library for python >= 3.7
GNU Lesser General Public License v3.0
1.04k stars 345 forks source link

Stuck in obtaining the read_data_type() of node. Related to asyncio feature. #1612

Closed DinoFra closed 2 months ago

DinoFra commented 2 months ago

Describe the bug
The process of acquiring a read_data_type() of node stayed stuck and i don't know why. I don't know if it's my dumb approach.

To Reproduce
I have a module python (x.py) that calls the object My_OPCUA_Client, as follow: Screenshot 2024-04-09 094118 - Copy (2) My Object My_OPCUA_Client. image After creating the OPCUA client instance i call from x.py the function getNodeId() which is async, as shown above: image

After that, the python interpreter stayed stuck. The result that i obtained is: image So, it starts the awaitable coroutine read_data_type() but doesn't return.

Expected behavior
Obtaining the data type of node asynchronously

Version
Python-Version:3.11.5 asyncua:1.0.4 asyncio:1.5.8

Thanks for any help.

oroulet commented 2 months ago

you need to do a bit more asyncio training. you cannot use asyncio.run that way

DinoFra commented 2 months ago

Hello, could you give me some tip? Thank you

schroeder- commented 2 months ago
  1. First dont post screenshoots, always add code as text. Also log outputs. It's hard to read and understand screenshoots.
  2. read about asyncio
  3. Here a incomplete example to use asyncio for your code: Remove asyncio.run(create_client()) from My_OPC_UA_Client init
async def run_client():
    client = My_OPC_UA_Client():
    await client.create_client()
    await client.get_node_ide("id")

if __name__ == "__main__":
    asyncio.run(run_client())
DinoFra commented 2 months ago

Sorry for the screenshots. I have read about that, the option mentioned only works if i want to run the script as the initial program entry point which doesn't happen.
I think the flow of my code, finishes the first asyncio.run() (when the object is created), i.e. the first event loop is closed, and after goes to the second asyncio.run() to execute the async function "getNodeId". I am not understanding the error.

oroulet commented 2 months ago

you need to have only one asyncio loop for the entire session (roughly). in a big application the the loop should be running already. Sounds like you in reality want to write sync code. Use the sync wrapper then