如何在协同路由中使用pymodbus.client.asynchronous.tcp中的AsynchmodBustCPClient?

如何在协同路由中使用pymodbus.client.asynchronous.tcp中的AsynchmodBustCPClient?,client,python-asyncio,coroutine,python-3.8,pymodbus,Client,Python Asyncio,Coroutine,Python 3.8,Pymodbus,基于PyModbus的“Async Asyncio Client Example”,我尝试在协同路由中初始化客户端。 run\u和\u已经运行\u loop()中的示例工作正常,但在协同程序async\u read()中运行时,初始化ModbusClient挂起,没有超时或错误消息 #!/usr/bin/env python import asyncio import logging from pymodbus.client.asynchronous.tcp import AsyncModbus

基于PyModbus的“Async Asyncio Client Example”,我尝试在协同路由中初始化客户端。 run\u和\u已经运行\u loop()中的示例工作正常,但在协同程序async\u read()中运行时,初始化ModbusClient挂起,没有超时或错误消息

#!/usr/bin/env python
import asyncio
import logging
from pymodbus.client.asynchronous.tcp import AsyncModbusTCPClient as ModbusClient
from pymodbus.client.asynchronous import schedulers

from threading import Thread
import time
# --------------------------------------------------------------------------- #
# configure the client logging
# --------------------------------------------------------------------------- #

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

UNIT = 0x01

TCP_IP = '192.168.0.168'
ADDRESS = 40035
COUNTS = 16


async def start_async_test(client):
    rr = await client.read_holding_registers(ADDRESS, COUNTS, unit=UNIT)
    print(rr.registers)


def run_with_already_running_loop():
    """
    An already running loop is passed to ModbusClient Factory
    :return:
    """
    log.debug("Running Async client with asyncio loop already started")
    log.debug("------------------------------------------------------")

    def done(future):
        log.info("future: Done !!!")

    def start_loop(loop):
        """
        Start Loop
        :param loop:
        :return:
        """
        asyncio.set_event_loop(loop)
        loop.run_forever()

    loop = asyncio.new_event_loop()
    t = Thread(target=start_loop, args=[loop])
    t.daemon = True
    # Start the loop
    t.start()
    assert loop.is_running()
    loop, client = ModbusClient(schedulers.ASYNC_IO,
                                host=TCP_IP,
                                loop=loop)
    future = asyncio.run_coroutine_threadsafe(
        start_async_test(client.protocol), loop=loop)
    future.add_done_callback(done)
    while not future.done():
        print('sleep')
        time.sleep(0.2)
    loop.stop()
    log.debug("--------DONE RUN_WITH_ALREADY_RUNNING_LOOP-------------")


async def async_read():
    """
    An already running loop is passed to ModbusClient Factory
    :return:
    """
    log.debug("Running Async client in async function")
    log.debug("------------------------------------------------------")
    loop = asyncio.get_running_loop()
    assert loop.is_running()
    # python hangs when initialising client
    loop, client = ModbusClient(schedulers.ASYNC_IO,
                                host=TCP_IP,
                                loop=loop)

    future = asyncio.run_coroutine_threadsafe(
        start_async_test(client.protocol), loop=loop)
    log.debug("------- DONE IN ASYNC FUNCTION -------------")
    log.debug("")


if __name__ == '__main__':
    log.debug(
        "------------------- Run with already running loop -------------------")
    run_with_already_running_loop()
    print('new test'.center(90, '-'))
    asyncio.run(async_read())

工作同步代码为

from pymodbus.client.sync import ModbusTcpClient

client = ModbusTcpClient(TCP_IP)
client.connect()
client.read_holding_registers(ADDRESS, count=COUNT)

知道如何使用asyncio实现类似的简单解决方案吗?

我遇到了同样的问题。我刚刚制造了一个问题:我遇到了同样的问题。我刚刚创造了一个问题:和一个公关