Skip to content

Commit

Permalink
Increase the poll interval to reduce CPU consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
StdioA authored and attzonko committed Feb 7, 2025
1 parent 776b4bb commit 9f8b408
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
3 changes: 1 addition & 2 deletions mmpy_bot/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ async def _check_queue_loop(self, webhook_queue: queue.Queue):
event = webhook_queue.get_nowait()
await self._handle_webhook(event)
except queue.Empty:
pass
await asyncio.sleep(0.0001)
await asyncio.sleep(0.5)

async def _handle_event(self, data):
post = json.loads(data)
Expand Down
2 changes: 1 addition & 1 deletion mmpy_bot/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def start_server():
await webhook_server.start()
while self.alive:
# We just use this to keep the loop running in a non-blocking way
await asyncio.sleep(0.001)
await asyncio.sleep(1)
await webhook_server.stop()
log.info("Webhook server thread stopped.")

Expand Down
3 changes: 1 addition & 2 deletions mmpy_bot/webhook_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ async def _obtain_responses_loop(self):
# If this handler already received a response, we can skip this.
pass
except Empty:
pass
await asyncio.sleep(0.0001)
await asyncio.sleep(0.5)

@handle_json_error
async def process_webhook(self, request: web.Request):
Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/webhook_server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_start(self, threadpool):
server = WebHookServer(port=3281, url=Settings().WEBHOOK_HOST_URL)
threadpool.start_webhook_server_thread(server)
threadpool.start()
time.sleep(0.5)
time.sleep(1)
assert server.running

asyncio.set_event_loop(asyncio.new_event_loop())
Expand All @@ -48,13 +48,13 @@ def test_obtain_response(self, server):
# We have no futures waiting for request id 'nonexistent', so nothing should
# happen.
server.response_queue.put(("nonexistent", None))
time.sleep(0.1)
time.sleep(1)
assert not server.response_handlers["test"].done()

# If a response comes in for request id 'test', it should be removed from the
# response handlers dict.
server.response_queue.put(("test", None))
time.sleep(0.1)
time.sleep(1)
assert "test" not in server.response_handlers

@pytest.mark.skip("Called from test_start since we can't parallellize this.")
Expand Down Expand Up @@ -86,7 +86,7 @@ async def send_request(data):

# Since there is no MessageHandler, we have to signal the server ourselves
server.response_queue.put((event.request_id, NoResponse))
time.sleep(0.1)
time.sleep(1)
# Upon receiving the NoResponse, the server should have emptied the response
# queue and handlers.
assert server.response_queue.empty()
Expand Down

0 comments on commit 9f8b408

Please sign in to comment.