提问人:North Legion 提问时间:11/10/2023 最后编辑:MaratNorth Legion 更新时间:11/10/2023 访问量:54
Python Django 和并行进程
Python Django and parallel process
问:
我正在 Django 中制作延迟消息传递服务。我到达了任务管理器。 从理论上讲,它应该与 Django 并行启动,它的任务是“读取数据库”并发送消息。 我使用了“multiprocessing Pool”,代码被输入到 manage.py 中。
让我感到困惑的是,启动时,其中一个进程会触发两次,在屏幕截图中为“ok”。 使用并行过程,站点已组装并正常工作。
为什么?有没有更好的做法?
结果:
не ok
не ok
Performing system checks...
Watching for file changes with StatReloader
System check identified no issues (0 silenced).
November 10, 2023 - 16:55:45
Django version 4.2.6, using settings 'WhiteRabbit.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
ok
2023-11-10 16:56:44.743514
ok
2023-11-10 16:56:45.275028
manage.py
:
import os
import sys
from multiprocessing import Pool
from automation.task_messanger import task_messanger
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'WhiteRabbit.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
with Pool(processes=2) as pool:
pool.apply_async(task_messanger, )
pool.apply_async(main(), )
# main()
我尝试了不同的泳池选项。
答: 暂无答案
评论
main()
()
pool.apply_async(main(), )
task_messanger
manage.py
manage.py migrate
makemigrations
task_messanger