Determine if app initialization should be skipped in this process. Returns True if: - A management command is being run (migrate, celery, shell, etc.) - The development server (daphne) is running - This is a worker process (not the master) This prevents redundant initializ
()
| 20 | |
| 21 | |
| 22 | def should_skip_initialization(): |
| 23 | """ |
| 24 | Determine if app initialization should be skipped in this process. |
| 25 | |
| 26 | Returns True if: |
| 27 | - A management command is being run (migrate, celery, shell, etc.) |
| 28 | - The development server (daphne) is running |
| 29 | - This is a worker process (not the master) |
| 30 | |
| 31 | This prevents redundant initialization across multiple worker processes. |
| 32 | """ |
| 33 | # Skip management commands and background services |
| 34 | skip_commands = [ |
| 35 | 'celery', 'beat', 'migrate', 'makemigrations', 'shell', 'dbshell', |
| 36 | 'collectstatic', 'loaddata' |
| 37 | ] |
| 38 | if any(cmd in sys.argv for cmd in skip_commands): |
| 39 | logger.debug(f"Skipping initialization due to command: {sys.argv}") |
| 40 | return True |
| 41 | |
| 42 | # Skip daphne development server (single process, no need to guard) |
| 43 | if 'daphne' in sys.argv[0] if sys.argv else False: |
| 44 | logger.debug(f"Skipping initialization in daphne development server. Command: {sys.argv}") |
| 45 | return True |
| 46 | |
| 47 | # Skip if this is a worker process spawned by uwsgi/gunicorn |
| 48 | if _is_worker_process(): |
| 49 | logger.debug(f"Skipping initialization in worker process. Command: {sys.argv}") |
| 50 | return True |
| 51 | |
| 52 | return False |
no test coverage detected