title: How to use multiprocess in django command?
body: <p>I'm tring to use <code>ProcessPoolExecutor</code> in django command to get some results at same time. And I tried with below codes to get it</p>
  <pre><code># main codes
  
  
  import json
  import time
  import datetime
  from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
  
  
  from redis import Redis
  from django.conf import settings
  from django.core.management.base import BaseCommand
  from thrift.transport import TSocket, TTransport
  from thrift.protocol import TBinaryProtocol
  
  from utils.cache import pool
  from services.analysis.thrift.Analysis import Client, Dashparam
  from api.analysis.models import MDashBoard
  
  
  redis_con = Redis(connection_pool=pool)
  
  class AnalysisThriftService(object):
  
      def __init__(self):
          ...
  
      def __enter__(self):
          return self
  
      def __exit__(self, exc_type, exc_val, exc_tb):
          self.transport.close()
  
  
  
  class Command(BaseCommand):
      help = 'python manage.py --settings &quot;xxx&quot;'
  
      def add_arguments(self, parser):
          parser.add_argument('--dashboard_id', type=int, help=&quot;ID&quot;)
  
      @staticmethod
      def _handle_with_thrift(dashboard_id):
          try:
              print(dashboard_id)
              with AnalysisThriftService() as thrift_server:
                  dashboard_result = ...
                  
          except:
              import traceback
              traceback.print_exc()
  
      def handle(self, *args, **options):
          dashboard_id = options[&quot;dashboard_id&quot;]
          if dashboard_id is None:
              dashboard_tables = [dashboard.id for dashboard in MDashBoard.objects.all()]
              with ProcessPoolExecutor(max_workers=5) as executor:
                  executor.map(Command._handle_with_thrift, dashboard_tables)
  
          else:
              ...
  
  </code></pre>
  <p>But I always get error like</p>
  <pre><code>Process Process-5:
  Process Process-2:
  Traceback (most recent call last):
    File &quot;D:\python3\lib\multiprocessing\process.py&quot;, line 258, in _bootstrap
      self.run()
    File &quot;D:\python3\lib\multiprocessing\process.py&quot;, line 93, in run
      self._target(*self._args, **self._kwargs)
    File &quot;D:\python3\lib\concurrent\futures\process.py&quot;, line 169, in _process_worker
      call_item = call_queue.get(block=True)
    File &quot;D:\python3\lib\multiprocessing\queues.py&quot;, line 113, in get
      return _ForkingPickler.loads(res)
    File &quot;C:\Users\Domob\Desktop\dev\myapi\analysis\management\commands\dashboard_schedule_task.py&quot;, line 15, in &lt;modu
  le&gt;
      from api.analysis.models import MDashBoard
    File &quot;C:\Users\Domob\Desktop\dev\myapi\analysis\models.py&quot;, line 4, in &lt;module&gt;
      from utils.models import BasicModel, StaticCharField
    File &quot;C:\Users\Domob\Desktop\dev\myapi\utils\models.py&quot;, line 9, in &lt;module&gt;
      class BasicModel(models.Model):
    File &quot;C:\Users\Domob\Desktop\dev\venv_myapi\lib\site-packages\django\db\models\base.py&quot;, line 103, in __new__
      app_config = apps.get_containing_app_config(module)
    File &quot;C:\Users\Domob\Desktop\dev\venv_myapi\lib\site-packages\django\apps\registry.py&quot;, line 252, in get_containing_ap
  p_config
      self.check_apps_ready()
    File &quot;C:\Users\Domob\Desktop\dev\venv_myapi\lib\site-packages\django\apps\registry.py&quot;, line 135, in check_apps_ready
      raise AppRegistryNotReady(&quot;Apps aren't loaded yet.&quot;)
  django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
  </code></pre>
  <p>How can I get the expected results.</p>
  <p>Great thanks.</p>