FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.36k stars 658 forks source link

BaseEventLoop._create_server_getaddrinfo #1086

Open Jayant-Pancholi opened 4 years ago

Jayant-Pancholi commented 4 years ago

I have Initialized all async functions in a class, Calling them main using asyncio.run(func()). Getting error for Server Start ()

RuntimeError: Task <Task pending name='Task-3' coro=<ServerAsyncUA_0207.StartServer() running at D:/WORKSPACE/OPC/modbus_opcua_SINGLEMAC_Setup/opcua_server/scratch_8.py:131> cb=[_run_until_complete_cb() at C:\Users\jayant.pancholi\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py:184]> got Future <_GatheringFuture pending> attached to a different loop sys:1: RuntimeWarning: coroutine 'BaseEventLoop._create_server_getaddrinfo' was never awaited

Calling it as like -

if name == 'main': main()

def main(): SAO = ServerAsyncUA_0207()

asyncio.run(SAO.StartServer())

async def StartServer(self): async with self.server: while 1: for each in self.var_list: await asyncio.sleep(0.1) await each.set_value(random.randint(1, 100))

swamper123 commented 4 years ago

I think you may want to use opcua-asyncio instead of python-opcua.

swamper123 commented 4 years ago

And I am a bit confused what ServerAsyncUA_0207 is. Have you used an alias import?

Jayant-Pancholi commented 4 years ago

Thanks for the input, But I am using asyncio and asyncua to implement server.

Jayant-Pancholi commented 4 years ago

ServerAsyncUA_0207 is a Class name in which I have various async defined functions.

swamper123 commented 4 years ago

Okay. Well your error message says, that 'BaseEventLoop._create_server_getaddrinfo' is missing an await.

Jayant-Pancholi commented 4 years ago

But that is coming from lib\asyncio\base_events.py this path. Library specific. what is that It is looking for. Is it like there are multiple async used.

Error also reads as -

RuntimeError: Task <Task pending name='Task-3' coro=<ServerAsyncUA_0207.StartServer() running at D:/WORKSPACE/OPC/modbus_opcua_SINGLEMAC_Setup/opcua_server/scratch_8.py:131> cb=[_run_until_complete_cb() at C:\Users\jayant.pancholi\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py:184]> got Future <_GatheringFuture pending> attached to a different loop

swamper123 commented 4 years ago

tbh I still don't know what your issue has to do with this repo(python-opcua), especially when opcua-asyncio is the opc server which uses async stuff.

Because I don't know your code, I can just tell you, that a future object/coroutine is missing an await.

Also I can say, that you may have a look at this example.

in short:

async def main():
    [...]
    server = Server()
    await server.init()
    [...]
    async with server:
        [...]

This is how opcua-asyncio deals with starting a server.

Jayant-Pancholi commented 4 years ago

how does "async with server" works ?

swamper123 commented 4 years ago

async with is new since python 3.6 I guess. Here the link to the docs, at the bottom. You need two more private methods in your server class: __aenter__ and __aexit__, If you enter an async statement, like async with server:,__aenter__ will be triggered, and when you get out of it, __aexit__.

You can see it here how they implemented it.

FYI: Even async for statements exist.