From 533bd36572dc0e6476425717499f1c2248160ec4 Mon Sep 17 00:00:00 2001 From: Michael Stilkerich Date: Sat, 5 Aug 2023 20:58:34 +0200 Subject: [PATCH 1/2] Fix CPU load of dockerapi container Previously the handle_pubsub_messages() loop was executing every 10ms when there was no message available. Now reading from the redis network socket will block (the coroutine) for up to 30s before it returns when no message is available. Using channel.listen() would be even better, but it lacks the ignore_subscribe_messages option and I could not figure out how to filter the returned messages. --- data/Dockerfiles/dockerapi/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/Dockerfiles/dockerapi/main.py b/data/Dockerfiles/dockerapi/main.py index 59d1a8ad..453a059d 100644 --- a/data/Dockerfiles/dockerapi/main.py +++ b/data/Dockerfiles/dockerapi/main.py @@ -198,8 +198,8 @@ async def handle_pubsub_messages(channel: aioredis.client.PubSub): while True: try: - async with async_timeout.timeout(1): - message = await channel.get_message(ignore_subscribe_messages=True) + async with async_timeout.timeout(60): + message = await channel.get_message(ignore_subscribe_messages=True, timeout=30) if message is not None: # Parse message data_json = json.loads(message['data'].decode('utf-8')) From 930473a980051ef1931ae5c4f57bedb24d56745e Mon Sep 17 00:00:00 2001 From: Michael Stilkerich Date: Sat, 12 Aug 2023 07:20:56 +0200 Subject: [PATCH 2/2] Set asyncio timeout to 0 for yielding --- data/Dockerfiles/dockerapi/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/Dockerfiles/dockerapi/main.py b/data/Dockerfiles/dockerapi/main.py index 453a059d..f9f02b63 100644 --- a/data/Dockerfiles/dockerapi/main.py +++ b/data/Dockerfiles/dockerapi/main.py @@ -244,7 +244,7 @@ async def handle_pubsub_messages(channel: aioredis.client.PubSub): else: dockerapi.logger.error("Unknwon PubSub recieved - %s" % json.dumps(data_json)) - await asyncio.sleep(0.01) + await asyncio.sleep(0.0) except asyncio.TimeoutError: pass