Skip to content

Commit

Permalink
Be able to create asyncio modbusclients from within a coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilv2 committed Feb 11, 2021
1 parent 7e8e7cf commit fc414f9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pymodbus/client/asynchronous/factory/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion pymodbus/client/asynchronous/factory/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pymodbus/client/asynchronous/factory/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pymodbus/client/asynchronous/factory/udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fc414f9

Please sign in to comment.