Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Django+Nginx+uWSGI timing Task

Shulou Source: shulou.com Published: 2022-06-02 00:01:12 09月15日 Update

Today, the editor will share with you the relevant knowledge points about how to achieve Django+Nginx+uWSGI timing tasks. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Abstract

When Nginx and uWSGI are not configured, use apscheduler to set scheduled tasks separately in url.py, and use python manage.py run server to run normally. However, after configuring uWSGI, the portal changes from manage.py to uwsgi.py, which requires user access to load apscheduler scheduled tasks of url.py, and starts repeatedly at a certain time with the number of user visits.

Cron using uWSGI

Method 1: migrate the apscheduler scheduled task of url.py to uwgsi.py

Method 2: use uWSGI's cron

UWSGI's cron official website: https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/Cron.html

End=1while end: try: import uwsgi / / set the job_id as 0 and start the timer of fuc at 12:12 every day.-1 represents * (all) uwsgi.register_signal (0, ", fuc) uwsgi.add_cron (0,12, 12) end=0 except: pass

Either method 1 or method 2 needs to set the worker=1 of uwsgi.ini

[uwsgi] # number of processes workers=1 uses socket.bind lock

Cron using uWSGI is only used in the case of a single process. If multiple processes will cause timer to start repeatedly, you can use socket.bind lock to modify the scheduled task.

Try: import socket sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) sock.bind (("127.0.0.1", 47200)) except socket.error: logger.info ("disable successful") else: / / scheduled task method

There is a problem, and it is possible to obtain sock.bind at the same time ("127.0.0.1", 47200), which can slow down the repetitive problem and cannot be solved completely.

Mule using uWSGI

Step 1: create a new Package and write _ _ init__.py

/ / if it is a Django project, you need to add it before you can use django's model//import django//os.environ.setdefault ('DJANGO_SETTINGS_MODULE',' project name. Settings') / / django.setup () from apscheduler.schedulers.background import BackgroundSchedulerscheduler = BackgroundScheduler () / / scheduler.add_job is not detailed For details, see the official website document scheduler.add_job (..., timezone='Asia/Shanghai') scheduler.start () try: import uwsgi while True: sig = uwsgi.signal_wait () except Exception as err: pass

Step 2: set uwsgi.ini and add mule = package package name / init.py

[uwsgi] mule = package package name / _ _ init__.py above is all the content of this article "how to implement Django+Nginx+uWSGI scheduled tasks". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Tasks methods knowledge articles processes problems content timers users projects configurations different large successful number representatives entrances and for the most part Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei Redmi macOS MySQL