三、Celery 定時任務
celery支援定時任務,設定好任務的執行時間,celery就會定時自動幫你執行, 這個定時任務子產品叫celery beat
寫一個腳本 叫periodic_task.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
add_periodic_task 會添加一條定時任務
上面是通過調用函數添加定時任務,也可以像寫配置檔案 一樣的形式添加, 下面是每30s執行的任務
1 2 3 4 5 6 7 8 | |
任務添加好了,需要讓celery單獨啟動一個程序來定時發起這些任務, 注意, 這裡是發起任務,不是執行,這個程序隻會不斷的去檢查你的任務計劃, 每發現有任務需要執行了,就發起一個任務調用消息,交給celery worker去執行
啟動任務排程器 celery beat
1 | |
輸出like below
1 2 3 4 5 6 7 8 9 10 | |
此時還差一步,就是還需要啟動一個worker,負責執行celery beat發起的任務
啟動celery worker來執行任務
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
好啦,此時觀察worker的輸出,是不是每隔一小會,就會執行一次定時任務呢!
注意:Beat needs to store the last run times of the tasks in a local database file (named celerybeat-schedule by default), so it needs access to write in the current directory, or alternatively you can specify a custom location for this file:
1 | |
更複雜的定時配置
上面的定時任務比較簡單,隻是每多少s執行一個任務,但如果你想要每周一三五的早上8點給你發郵件怎麼辦呢?哈,其實也簡單,用crontab功能,跟linux自帶的crontab功能是一樣的,可以個性化定制任務執行時間
linux crontab http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html
1 2 3 4 5 6 7 8 9 10 | |
上面的這條意思是每周1的早上7.30執行tasks.add任務
還有更多定時配置方式如下:
Example | Meaning |
| Execute every minute. |
| Execute daily at midnight. |
| Execute every three hours: midnight, 3am, 6am, 9am, noon, 3pm, 6pm, 9pm. |
| Same as previous. |
| Execute every 15 minutes. |
| Execute every minute (!) at Sundays. |
| Same as previous. |
| Execute every ten minutes, but only between 3-4 am, 5-6 pm, and 10-11 pm on Thursdays or Fridays. |
| Execute every even hour, and every hour divisible by three. This means: at every hour except: 1am, 5am, 7am, 11am, 1pm, 5pm, 7pm, 11pm |
| Execute hour divisible by 5. This means that it is triggered at 3pm, not 5pm (since 3pm equals the 24-hour clock value of “15”, which is divisible by 5). |
| Execute every hour divisible by 3, and every hour during office hours (8am-5pm). |
| Execute on the second day of every month. |
| Execute on every even numbered day. |
| Execute on the first and third weeks of the month. |
| Execute on the eleventh of May every year. |
| Execute on the first month of every quarter. |
上面能滿足你絕大多數定時任務需求了,甚至還能根據潮起潮落來配置定時任務, 具體看 http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#solar-schedules
四、最佳實踐之與django結合
django 可以輕松跟celery結合實作異步任務,隻需簡單配置即可
If you have a modern Django project layout like:
- proj/
- proj/__init__.py - proj/settings.py - proj/urls.py - manage.py
then the recommended way is to create a new proj/proj/celery.py module that defines the Celery instance:
file: proj/proj/celery.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Then you need to import this app in your
proj/proj/__init__.py
module. This ensures that the app is loaded when Django starts so that the
@shared_task
decorator (mentioned later) will use it:
proj/proj/__init__.py
:
1 2 3 4 5 6 7 | |
Note that this example project layout is suitable for larger projects, for simple projects you may use a single contained module that defines both the app and tasks, like in the First Steps with Celery tutorial.
Let’s break down what happens in the first module, first we import absolute imports from the future, so that our
celery.py
module won’t clash with the library:
1 | |
Then we set the default
DJANGO_SETTINGS_MODULE
environment variable for the celery command-line program:
1 | |
You don’t need this line, but it saves you from always passing in the settings module to the
celery
program. It must always come before creating the app instances, as is what we do next:
1 | |
This is our instance of the library.
We also add the Django settings module as a configuration source for Celery. This means that you don’t have to use multiple configuration files, and instead configure Celery directly from the Django settings; but you can also separate them if wanted.
The uppercase name-space means that all Celery configuration options must be specified in uppercase instead of lowercase, and start with
CELERY_
, so for example the
task_always_eager`
setting becomes
CELERY_TASK_ALWAYS_EAGER
, and the
broker_url
setting becomes
CELERY_BROKER_URL
.
You can pass the object directly here, but using a string is better since then the worker doesn’t have to serialize the object.
1 | |
Next, a common practice for reusable apps is to define all tasks in a separate
tasks.py
module, and Celery does have a way to auto-discover these modules:
1 | |
With the line above Celery will automatically discover tasks from all of your installed apps, following the
tasks.py
convention:
1 2 3 4 5 6 | |
Finally, the
debug_task
example is a task that dumps its own request information. This is using the new
bind=True
task option introduced in Celery 3.1 to easily refer to the current task instance.
然後在具體的app裡的tasks.py裡寫你的任務
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
在你的django views裡調用celery task
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
五、在django中使用計劃任務功能
There’s the django-celery-beat extension that stores the schedule in the Django database, and presents a convenient admin interface to manage periodic tasks at runtime.
To install and use this extension:
- Use pip to install the package:
$ pip install django-celery-beat
- Add the
module todjango_celery_beat
in your Django project’INSTALLED_APPS
:settings.py
INSTALLED_APPS = ( ..., 'django_celery_beat', ) Note that there is no dash in the module name, only underscores.
- Apply Django database migrations so that the necessary tables are created:
$ python manage.py migrate
- Start the celery beat service using the
scheduler:django
$ celery -A proj beat -l info -S django
- Visit the Django-Admin interface to set up some periodic tasks.
在admin頁面裡,有3張表
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iN0MDN2gjNwkTMtcDMzYDNwIzMxkDMyAzNxAjMtMzMzAjM38CXyAzNxAjMvw1MzMDMyczLcd2bsJ2Lc12bj5ycn9Gbi52YuUTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
配置完長這樣
此時啟動你的celery beat 和worker,會發現每隔2分鐘,beat會發起一個任務消息讓worker執行scp_task任務
注意,經測試,每添加或修改一個任務,celery beat都需要重新開機一次,要不然新的配置不會被celery beat程序讀到
轉載于:https://www.cnblogs.com/now-playing/p/8334001.html