In Django view, I'm trying to run a process as shown below. *I use Django 3.1.7 and Python 3.9.13:
# "views.py"
from multiprocessing import Process
from django.http import HttpResponse
def test():
print("Test")
def call_test(request):
process = Process(target=test)
process.start()
process.join()
return HttpResponse("Test")
But, I got this error below:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
So, following this answer, I added django.setup()
and used ProcessPoolExecutor()
as shown below:
# "views.py"
import django
import concurrent.futures
from django.http import HttpResponse
def test():
django.setup()
print("Test")
def call_test(request):
django.setup()
with concurrent.futures.ProcessPoolExecutor() as executor:
feature = executor.submit(test)
return HttpResponse("Test")
But, the error is not solved as shown below:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
So, how can I run a process in Django view without Apps aren't loaded yet
error?