多处理.Process.start()引发异常django.core.exceptions.AppRegistryNot



Views.py

def time_consuming_task_caller(time_to_sleep, j):
print ('start', task_id)
# time.sleep(time_to_sleep)
# print('finished', task_id)

def time_consuming_task_caller(time_to_sleep, j):
Process(target=time_consuming_task, args=(time_to_sleep, task_id)).start()
print ('processing...', task_id)
return {'one': 'two'}

def get_task(request):
if request.method == 'GET':
response = time_consuming_task_caller(5,1) # 5 is like time to sleep, 1 is task id.
print(response)
return HttpResponse(json.dumps(response), content_type="application/json")

这导致return HttpResponse(json.dumps(response), content_type="application/json")出现错误,因为它打印了所有三个打印语句(注释语句除外(

即使对time.sleep()进行了注释,也会导致错误。最终目标是使其适用于可能需要更长时间执行的流程。

编辑

urls.py

urlpatterns = [
path('api/', get_task ,name='run')
]

错误

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:usersuserappdatalocalprogramspythonpython36Libmultiprocessingspawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "c:usersuserappdatalocalprogramspythonpython36Libmultiprocessingspawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
File "D:Programmingdjango_projectwebapp - Devserver 3managerutils.py", line 1, in <module>
from .models import Task
File "D:Programmingdjango_projectwebapp - Devserver 3managermodels.py", line 2, in <module>
from accounts.models import CustomUser
File "D:Programmingdjango_projectwebapp - Devserver 3accountsmodels.py", line 2, in <module>
from django.contrib.auth.models import AbstractUser
File "D:Programmingdjango_projectenv_djangolibsite-packagesdjangocontribauthmodels.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "D:Programmingdjango_projectenv_djangolibsite-packagesdjangocontribauthbase_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "D:Programmingdjango_projectenv_djangolibsite-packagesdjangodbmodelsbase.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "D:Programmingdjango_projectenv_djangolibsite-packagesdjangoappsregistry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "D:Programmingdjango_projectenv_djangolibsite-packagesdjangoappsregistry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

从您提供的堆栈跟踪中,错误也可能是由models文件(您没有包括该文件(引起的。

但实现此类功能的正确方法是使用类似芹菜

相关内容

  • 没有找到相关文章

最新更新