Skip to content

Commit 9f8b408

Browse files
StdioAattzonko
authored andcommitted
Increase the poll interval to reduce CPU consumption
1 parent 776b4bb commit 9f8b408

File tree

4 files changed

+7
-9
lines changed

4 files changed

+7
-9
lines changed

mmpy_bot/event_handler.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ async def _check_queue_loop(self, webhook_queue: queue.Queue):
4848
event = webhook_queue.get_nowait()
4949
await self._handle_webhook(event)
5050
except queue.Empty:
51-
pass
52-
await asyncio.sleep(0.0001)
51+
await asyncio.sleep(0.5)
5352

5453
async def _handle_event(self, data):
5554
post = json.loads(data)

mmpy_bot/threadpool.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async def start_server():
9393
await webhook_server.start()
9494
while self.alive:
9595
# We just use this to keep the loop running in a non-blocking way
96-
await asyncio.sleep(0.001)
96+
await asyncio.sleep(1)
9797
await webhook_server.stop()
9898
log.info("Webhook server thread stopped.")
9999

mmpy_bot/webhook_server.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ async def _obtain_responses_loop(self):
8181
# If this handler already received a response, we can skip this.
8282
pass
8383
except Empty:
84-
pass
85-
await asyncio.sleep(0.0001)
84+
await asyncio.sleep(0.5)
8685

8786
@handle_json_error
8887
async def process_webhook(self, request: web.Request):

tests/unit_tests/webhook_server_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_start(self, threadpool):
2424
server = WebHookServer(port=3281, url=Settings().WEBHOOK_HOST_URL)
2525
threadpool.start_webhook_server_thread(server)
2626
threadpool.start()
27-
time.sleep(0.5)
27+
time.sleep(1)
2828
assert server.running
2929

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

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

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

8787
# Since there is no MessageHandler, we have to signal the server ourselves
8888
server.response_queue.put((event.request_id, NoResponse))
89-
time.sleep(0.1)
89+
time.sleep(1)
9090
# Upon receiving the NoResponse, the server should have emptied the response
9191
# queue and handlers.
9292
assert server.response_queue.empty()

0 commit comments

Comments
 (0)