Frappe V16 Python Cache Issue in Production Server

I am facing a cache-related issue in Frappe V16 on a production server.

Environment

  • Frappe Version: v16

  • ERPNext Version: v16

  • Production Mode

  • Ubuntu Server

  • Using custom applications

Issue Description

Whenever I update Python code (API methods, class functions, or backend logic), the changes are not reflected immediately on the production server.

However:

  • JavaScript changes are getting updated properly after build/reload.

  • Python changes continue using the old code even after modification.

What I Tried

I already tried the following commands:

bench clear-cache
bench clear-website-cache
bench migrate
sudo supervisorctl restart all
bench restart

Even after this, sometimes the old Python code is still executed.

In some cases:

  • Restarting the entire server fixes the issue temporarily.

  • Clearing browser cache does not help.

  • Issue mainly happens with whitelisted methods/API functions and backend class methods.

Expected Behavior

Whenever Python files are updated in custom apps, the latest code should reflect immediately after restart/migrate.

Additional Information

  • Server has been running continuously for several months.

  • Using Gunicorn + Supervisor + Redis.

  • Multiple custom apps installed.

  • Production setup only has this issue; development mode works correctly.

Questions

  1. Is this related to Redis cache or Python import caching?

  2. Does Frappe V16 require additional restart steps for Python updates?

  3. Is there any recommended production deployment process to avoid stale Python code?

  4. Could this be caused by multiple workers or old processes not restarting correctly?

Please help if anyone faced similar issues in Frappe V16 production environments.

Thanks.

What was the output of the restart command?