diff --git a/pymodbus/client/asynchronous/factory/serial.py b/pymodbus/client/asynchronous/factory/serial.py index 62b719c5c4..b2b213046c 100644 --- a/pymodbus/client/asynchronous/factory/serial.py +++ b/pymodbus/client/asynchronous/factory/serial.py @@ -103,11 +103,11 @@ def async_io_factory(port=None, framer=None, **kwargs): client = AsyncioModbusSerialClient(port, proto_cls, framer, loop, **kwargs) coro = client.connect() - if loop.is_running(): + if not loop.is_running(): + loop.run_until_complete(coro) + elif loop is not asyncio.get_event_loop(): future = asyncio.run_coroutine_threadsafe(coro, loop=loop) future.result() - else: - loop.run_until_complete(coro) return loop, client diff --git a/pymodbus/client/asynchronous/factory/tcp.py b/pymodbus/client/asynchronous/factory/tcp.py index d0155a48e0..883696267f 100644 --- a/pymodbus/client/asynchronous/factory/tcp.py +++ b/pymodbus/client/asynchronous/factory/tcp.py @@ -91,12 +91,14 @@ def async_io_factory(host="127.0.0.1", port=Defaults.Port, framer=None, """ import asyncio from pymodbus.client.asynchronous.async_io import init_tcp_client - loop = kwargs.get("loop") or asyncio.new_event_loop() + loop = kwargs.get("loop") or asyncio.get_event_loop() proto_cls = kwargs.get("proto_cls", None) if not loop.is_running(): asyncio.set_event_loop(loop) cor = init_tcp_client(proto_cls, loop, host, port) client = loop.run_until_complete(asyncio.gather(cor))[0] + elif loop is asyncio.get_event_loop(): + client = init_tcp_client(proto_cls, loop, host, port) else: cor = init_tcp_client(proto_cls, loop, host, port) future = asyncio.run_coroutine_threadsafe(cor, loop=loop) diff --git a/pymodbus/client/asynchronous/factory/tls.py b/pymodbus/client/asynchronous/factory/tls.py index 3b11ebf5aa..b6df55900e 100644 --- a/pymodbus/client/asynchronous/factory/tls.py +++ b/pymodbus/client/asynchronous/factory/tls.py @@ -29,13 +29,15 @@ def async_io_factory(host="127.0.0.1", port=Defaults.TLSPort, sslctx=None, """ import asyncio from pymodbus.client.asynchronous.async_io import init_tls_client - loop = kwargs.get("loop") or asyncio.new_event_loop() + loop = kwargs.get("loop") or asyncio.get_event_loop() proto_cls = kwargs.get("proto_cls", None) if not loop.is_running(): asyncio.set_event_loop(loop) cor = init_tls_client(proto_cls, loop, host, port, sslctx, server_hostname, framer) client = loop.run_until_complete(asyncio.gather(cor))[0] + elif loop is asyncio.get_event_loop(): + return loop, init_tls_client(proto_cls, loop, host, port) else: cor = init_tls_client(proto_cls, loop, host, port, sslctx, server_hostname, framer) diff --git a/pymodbus/client/asynchronous/factory/udp.py b/pymodbus/client/asynchronous/factory/udp.py index 542ebedc3e..7b7e9e7487 100644 --- a/pymodbus/client/asynchronous/factory/udp.py +++ b/pymodbus/client/asynchronous/factory/udp.py @@ -68,10 +68,13 @@ def async_io_factory(host="127.0.0.1", port=Defaults.Port, framer=None, from pymodbus.client.asynchronous.async_io import init_udp_client loop = kwargs.get("loop") or asyncio.get_event_loop() proto_cls = kwargs.get("proto_cls", None) - cor = init_udp_client(proto_cls, loop, host, port) if not loop.is_running(): + cor = init_udp_client(proto_cls, loop, host, port) client = loop.run_until_complete(asyncio.gather(cor))[0] + elif loop is asyncio.get_event_loop(): + return loop, init_udp_client(proto_cls, loop, host, port) else: + cor = init_udp_client(proto_cls, loop, host, port) client = asyncio.run_coroutine_threadsafe(cor, loop=loop) client = client.result() return loop, client