[英]django how can I use multiprocessing in a management command
如何在 django 管理命令中使用多處理? 我不知道如何在我的網站 django 上下文中啟動該過程
管理命令
users = users.objects.filter(is_active=True)
with ProcessPoolExecutor(max_workers=5) as executor:
processes = []
for user in users:
p = executor.submit(complex_user_calculation,user)
processes.append(p)
for f in concurrent.futures.as_completed(processes):
result = f.result()
if result:
print(result)
當我運行此管理命令時,我收到此錯誤
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
...
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py", line 390, in __get_result
raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
您需要在每個子進程中初始化 Django 應用程序,以確保您的應用程序、模型等都已加載。 通過django.setup
作為您的池初始化程序
with ProcessPoolExecutor(max_workers=5, initializer=django.setup) as executor:
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.