|
25 | 25 | sys.stderr("This example needs to be run only on python 3.4 and above")
|
26 | 26 | sys.exit(1)
|
27 | 27 |
|
| 28 | +from threading import Thread |
| 29 | +import time |
28 | 30 | # --------------------------------------------------------------------------- #
|
29 | 31 | # configure the client logging
|
30 | 32 | # --------------------------------------------------------------------------- #
|
@@ -129,8 +131,81 @@ async def start_async_test(client):
|
129 | 131 | await asyncio.sleep(1)
|
130 | 132 |
|
131 | 133 |
|
132 |
| -if __name__ == '__main__': |
| 134 | +def run_with_not_running_loop(): |
| 135 | + """ |
| 136 | + A loop is created and is passed to ModbusClient factory to be used. |
133 | 137 |
|
| 138 | + :return: |
| 139 | + """ |
| 140 | + log.debug("Running Async client with asyncio loop not yet started") |
| 141 | + log.debug("------------------------------------------------------") |
| 142 | + loop = asyncio.new_event_loop() |
| 143 | + assert not loop.is_running() |
| 144 | + new_loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020, loop=loop) |
| 145 | + loop.run_until_complete(start_async_test(client.protocol)) |
| 146 | + loop.close() |
| 147 | + log.debug("--------------RUN_WITH_NOT_RUNNING_LOOP---------------") |
| 148 | + log.debug("") |
| 149 | + |
| 150 | + |
| 151 | +def run_with_already_running_loop(): |
| 152 | + """ |
| 153 | + An already running loop is passed to ModbusClient Factory |
| 154 | + :return: |
| 155 | + """ |
| 156 | + log.debug("Running Async client with asyncio loop already started") |
| 157 | + log.debug("------------------------------------------------------") |
| 158 | + |
| 159 | + def done(future): |
| 160 | + log.info("Done !!!") |
| 161 | + |
| 162 | + def start_loop(loop): |
| 163 | + """ |
| 164 | + Start Loop |
| 165 | + :param loop: |
| 166 | + :return: |
| 167 | + """ |
| 168 | + asyncio.set_event_loop(loop) |
| 169 | + loop.run_forever() |
| 170 | + |
| 171 | + loop = asyncio.new_event_loop() |
| 172 | + t = Thread(target=start_loop, args=[loop]) |
| 173 | + t.daemon = True |
| 174 | + # Start the loop |
| 175 | + t.start() |
| 176 | + assert loop.is_running() |
| 177 | + asyncio.set_event_loop(loop) |
| 178 | + loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020, loop=loop) |
| 179 | + future = asyncio.run_coroutine_threadsafe( |
| 180 | + start_async_test(client.protocol), loop=loop) |
| 181 | + future.add_done_callback(done) |
| 182 | + while not future.done(): |
| 183 | + time.sleep(0.1) |
| 184 | + loop.stop() |
| 185 | + log.debug("--------DONE RUN_WITH_ALREADY_RUNNING_LOOP-------------") |
| 186 | + log.debug("") |
| 187 | + |
| 188 | + |
| 189 | +def run_with_no_loop(): |
| 190 | + """ |
| 191 | + ModbusClient Factory creates a loop. |
| 192 | + :return: |
| 193 | + """ |
134 | 194 | loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020)
|
135 | 195 | loop.run_until_complete(start_async_test(client.protocol))
|
136 | 196 | loop.close()
|
| 197 | + |
| 198 | + |
| 199 | +if __name__ == '__main__': |
| 200 | + # Run with No loop |
| 201 | + log.debug("Running Async client") |
| 202 | + log.debug("------------------------------------------------------") |
| 203 | + run_with_no_loop() |
| 204 | + |
| 205 | + # Run with loop not yet started |
| 206 | + run_with_not_running_loop() |
| 207 | + |
| 208 | + # Run with already running loop |
| 209 | + run_with_already_running_loop() |
| 210 | + log.debug("---------------------RUN_WITH_NO_LOOP-----------------") |
| 211 | + log.debug("") |
0 commit comments