|
1 | 1 | import asyncio |
2 | | -import sys,os |
3 | | -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'src'))) |
| 2 | +import random |
| 3 | + |
4 | 4 | from iop import BusinessProcess |
5 | 5 | from msg import MyMessage |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class MyAsyncNGBP(BusinessProcess): |
9 | 9 |
|
10 | 10 | def on_message(self, request): |
11 | | - import time |
12 | | - start_time = time.time() |
| 11 | + |
13 | 12 | results = asyncio.run(self.await_response(request)) |
14 | | - end_time = time.time() |
15 | | - print(f"Time taken: {end_time - start_time} seconds") |
16 | | - self.log_info(f"Time taken: {end_time - start_time} seconds") |
| 13 | + |
17 | 14 | for result in results: |
18 | | - print(f"Received response: {result.message}") |
| 15 | + self.log_info(f"Received response: {result.message}") |
19 | 16 |
|
20 | 17 | async def await_response(self, request): |
21 | | - msg_one = MyMessage(message="Message1") |
22 | | - msg_two = MyMessage(message="Message2") |
23 | | - |
24 | | - # use asyncio.gather to send multiple requests asynchronously |
25 | | - # using the send_request_async_ng method |
26 | | - tasks = [self.send_request_async_ng("Python.MyAsyncNGBO", msg_one, timeout=5), |
27 | | - self.send_request_async_ng("Python.MyAsyncNGBO", msg_two, timeout=-1)] |
| 18 | + # create 1 to 10 messages |
| 19 | + tasks = [] |
| 20 | + for i in range(random.randint(1, 10)): |
| 21 | + tasks.append(self.send_request_async_ng("Python.MyAsyncNGBO", |
| 22 | + MyMessage(message=f"Message {i}"))) |
28 | 23 |
|
29 | 24 | return await asyncio.gather(*tasks) |
30 | 25 |
|
31 | | -if __name__ == "__main__": |
32 | | - bp = MyAsyncNGBP() |
33 | | - bp.on_message(None) |
0 commit comments