From 29d28a460f1e7bb14595e827b43c0ba9c57b8fe9 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sat, 24 Jun 2023 17:06:23 +0530 Subject: [PATCH] perf: Freeze GC right before starting background worker BG worker forks are not CoW friendly. Freezing right before we start worker should lessen overall memory usage. Though this isn't useful much because at max you're sharing with 2 processes - master and horse. WorkerPool can improve this benefit a lot by forking each worker from master process and horse from forked processes. TBD when WorkerPool is out of beta. --- frappe/utils/background_jobs.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/frappe/utils/background_jobs.py b/frappe/utils/background_jobs.py index ea3ea4c191..9b2ff329df 100755 --- a/frappe/utils/background_jobs.py +++ b/frappe/utils/background_jobs.py @@ -1,3 +1,4 @@ +import gc import os import socket import time @@ -234,6 +235,9 @@ def start_worker( """Wrapper to start rq worker. Connects to redis and monitors these queues.""" DEQUEUE_STRATEGIES = {"round_robin": RoundRobinWorker, "random": RandomWorker} + if frappe._tune_gc: + gc.freeze() + with frappe.init_site(): # empty init is required to get redis_queue from common_site_config.json redis_connection = get_redis_conn(username=rq_username, password=rq_password)