推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
IVeverKB
V2EX  ›  Python

django 开启一个线程应写在哪个文件下

  •  
  •   IVeverKB · Dec 8, 2020 · 2548 views
    This topic created in 2169 days ago, the information mentioned may be changed or developed.

    想在 django 运行的同时运行一个线程同步处理一些其他业务,那写在哪个文件比较合适?刚上手 django 不太了解程序的入口。

    首先肯定不能写在 manage.py 下,我暂时考虑的是项目下的__init__.py 或 wsgi.py 文件中不知是否合适,主要是想保证之后部署过程中这些文件在启动服务器之后只会被执行一次,且写在这里不会干涉 django 本身业务的运行。

    之前有看到写在 urls.py 、views.py 中会因为部署在 uwsgi+nginx 而产生多进程执行多次,想规避一下类似的问题。

    先提前感谢!!!

    7 replies  •  2020-12-09 15:15:33 +08:00
    ytymf
    ytymf
    Dec 8, 2020
    1
    django 本身不含任何的入口,只有一个 wsgi 的接口,它的生命周期归上层的 uwsgi/iis 之类的管。这种需求用 celery 搞定就行了。
    meiyoumingzi6
    meiyoumingzi6
    Dec 8, 2020
    2
    我理解你是想开一些定时任务或是异步任务这种?
    新开一个进程好了, 楼上推荐的 celery 就不错啊
    676529483
    676529483
    Dec 8, 2020
    3
    我一般写在 url/wsgi/apps 里面,但这些都不能避免 uwsgi 启动多进程,所以你还是单独启动一个进程吧
    tabris17
    tabris17
    Dec 8, 2020
    4
    现成的 django-celery 为什么不用?
    https://github.com/celery/django-celery
    rSP0rcaA9K11Rjsz
    rSP0rcaA9K11Rjsz
    Dec 9, 2020
    5
    随便新建个包就行了
    julyclyde
    julyclyde
    Dec 9, 2020
    6
    @tabris17 django-celery 我记得已经淘汰了好多年了啊,咋突然诈尸了??
    tabris17
    tabris17
    Dec 9, 2020
    7
    @julyclyde 哎?最近一次用 django 还是 1.8 呢,时代变了啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4431 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 08:42 · PVG 16:42 · LAX 00:42 · JFK 03:42
    ♥ Do have faith in what you're doing.