日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python Celery 分布式任务队列快速入门

發布時間:2025/3/21 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python Celery 分布式任务队列快速入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本節內容

Celery介紹和基本使用

在項目中如何使用celery

啟用多個workers

Celery 定時任務

與django結合

通過django配置celery periodic task

?

?

一、Celery介紹和基本使用?

Celery 是一個 基于python開發的分布式異步消息任務隊列,通過它可以輕松的實現任務的異步處理, 如果你的業務場景中需要用到異步任務,就可以考慮使用celery, 舉幾個實例場景中可用的例子:

  • 你想對100臺機器執行一條批量命令,可能會花很長時間 ,但你不想讓你的程序等著結果返回,而是給你返回 一個任務ID,你過一段時間只需要拿著這個任務id就可以拿到任務執行結果, 在任務執行ing進行時,你可以繼續做其它的事情。?
  • 你想做一個定時任務,比如每天檢測一下你們所有客戶的資料,如果發現今天 是客戶的生日,就給他發個短信祝福
  • ?

    Celery 在執行任務時需要通過一個消息中間件來接收和發送任務消息,以及存儲任務結果, 一般使用rabbitMQ or Redis,后面會講

    1.1 Celery有以下優點:

  • 簡單:一單熟悉了celery的工作流程后,配置和使用還是比較簡單的
  • 高可用:當任務執行失敗或執行過程中發生連接中斷,celery 會自動嘗試重新執行任務
  • 快速:一個單進程的celery每分鐘可處理上百萬個任務
  • 靈活: 幾乎celery的各個組件都可以被擴展及自定制
  • Celery基本工作流程圖

    ?

    ?

    1.2 Celery安裝使用

    Celery的默認broker是RabbitMQ, 僅需配置一行就可以

    1 broker_url?=?'amqp://guest:guest@localhost:5672//'

    rabbitMQ 沒裝的話請裝一下,安裝看這里 ?http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html#id3

    ?

    使用Redis做broker也可以

    安裝redis組件

    1 $ pip install?-U?"celery[redis]"

    配置

    Configuration is easy, just configure the location of your Redis database:

    app.conf.broker_url = 'redis://localhost:6379/0'

    Where the URL is in the format of:

    redis://:password@hostname:port/db_number

    all fields after the scheme are optional, and will default to?localhost?on port 6379, using database 0.

    ?

    如果想獲取每個任務的執行結果,還需要配置一下把任務結果存在哪

    If you also want to store the state and return values of tasks in Redis, you should configure these settings:

    app.conf.result_backend = 'redis://localhost:6379/0'

    ?

    1. 3 開始使用Celery啦  

    安裝celery模塊

    1 $ pip install celery

    ?

    創建一個celery application 用來定義你的任務列表

    創建一個任務文件就叫tasks.py吧

    1 2 3 4 5 6 7 8 9 10 from?celery?import?Celery app?=?Celery('tasks', ?????????????broker='redis://localhost', ?????????????backend='redis://localhost') @app.task def?add(x,y): ????print("running...",x,y) ????return?x+y

    ?

    啟動Celery Worker來開始監聽并執行任務

    1 $ celery -A tasks worker --loglevel=info

      

    調用任務

    再打開一個終端, 進行命令行模式,調用任務  

    1 2 >>>?from?tasks?import?add >>> add.delay(4,?4)

    看你的worker終端會顯示收到 一個任務,此時你想看任務結果的話,需要在調用 任務時 賦值個變量

    1 >>> result?=?add.delay(4,?4)

    The?ready()?method returns whether the task has finished processing or not:

    >>> result.ready() False

    You can wait for the result to complete, but this is rarely used since it turns the asynchronous call into a synchronous one:

    >>> result.get(timeout=1) 8

    In case the task raised an exception,?get()?will re-raise the exception, but you can override this by specifying the?propagate?argument:

    >>> result.get(propagate=False)

    If the task raised an exception you can also gain access to the original traceback:

    >>> result.traceback

      

    二、在項目中如何使用celery 

    可以把celery配置成一個應用

    目錄格式如下

    1 2 3 proj/__init__.py ????/celery.py ????/tasks.py

    proj/celery.py內容

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from?__future__?import?absolute_import, unicode_literals from?celery?import?Celery app?=?Celery('proj', ?????????????broker='amqp://', ?????????????backend='amqp://', ?????????????include=['proj.tasks']) # Optional configuration, see the application user guide. app.conf.update( ????result_expires=3600, ) if?__name__?==?'__main__': ????app.start()

    proj/tasks.py中的內容

    from __future__ import absolute_import, unicode_literals from .celery import app@app.task def add(x, y):return x + y@app.task def mul(x, y):return x * y@app.task def xsum(numbers):return sum(numbers)

    啟動worker?

    1 $ celery -A proj worker -l info

    輸出

    1 2 3 4 5 6 7 8 9 10 11 12 13 -------------- celery@Alexs-MacBook-Pro.local?v4.0.2 (latentcall) ---- **** ----- --- * ***? * -- Darwin-15.6.0-x86_64-i386-64bit 2017-01-26 21:50:24 -- * - **** --- - ** ---------- [config] - ** ---------- .> app:???????? proj:0x103a020f0 - ** ---------- .> transport:?? redis://localhost:6379// - ** ---------- .> results:???? redis://localhost/ - *** --- * --- .> concurrency: 8 (prefork) -- ******* ---- .> task events: OFF (enable?-E to monitor tasks?in?this worker) --- ***** ----- ?-------------- [queues] ????????????????.> celery?????????? exchange=celery(direct) key=celery

      

    后臺啟動worker

    In the background

    In production you’ll want to run the worker in the background, this is described in detail in the?daemonization tutorial.

    The daemonization scripts uses the?celery multi?command to start one or more workers in the background:

    $ celery multi start w1 -A proj -l info celery multi v4.0.0 (latentcall) > Starting nodes... > w1.halcyon.local: OK

    You can restart it too:

    $ celery multi restart w1 -A proj -l info celery multi v4.0.0 (latentcall) > Stopping nodes... > w1.halcyon.local: TERM -> 64024 > Waiting for 1 node..... > w1.halcyon.local: OK > Restarting node w1.halcyon.local: OK celery multi v4.0.0 (latentcall) > Stopping nodes... > w1.halcyon.local: TERM -> 64052

    or stop it:

    $ celery multi stop w1 -A proj -l info

    The?stop?command is asynchronous so it won’t wait for the worker to shutdown. You’ll probably want to use the?stopwait?command instead, this ensures all currently executing tasks is completed before exiting:

    $ celery multi stopwait w1 -A proj -l info

      

    ?

    三、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 from?celery?import?Celery from?celery.schedules?import?crontab app?=?Celery() @app.on_after_configure.connect def?setup_periodic_tasks(sender,?**kwargs): ????# Calls test('hello') every 10 seconds. ????sender.add_periodic_task(10.0, test.s('hello'), name='add every 10') ????# Calls test('world') every 30 seconds ????sender.add_periodic_task(30.0, test.s('world'), expires=10) ????# Executes every Monday morning at 7:30 a.m. ????sender.add_periodic_task( ????????crontab(hour=7, minute=30, day_of_week=1), ????????test.s('Happy Mondays!'), ????) @app.task def?test(arg): ????print(arg)

    add_periodic_task 會添加一條定時任務

    ?

    上面是通過調用函數添加定時任務,也可以像寫配置文件 一樣的形式添加, 下面是每30s執行的任務

    1 2 3 4 5 6 7 8 app.conf.beat_schedule?=?{ ????'add-every-30-seconds': { ????????'task':?'tasks.add', ????????'schedule':?30.0, ????????'args': (16,?16) ????}, } app.conf.timezone?=?'UTC'

      

    任務添加好了,需要讓celery單獨啟動一個進程來定時發起這些任務, 注意, 這里是發起任務,不是執行,這個進程只會不斷的去檢查你的任務計劃, 每發現有任務需要執行了,就發起一個任務調用消息,交給celery worker去執行

    啟動任務調度器 celery beat

    1 $ celery -A periodic_task beat

    輸出like below

    1 2 3 4 5 6 7 8 9 10 celery beat v4.0.2 (latentcall) is starting. __??? -??? ... __?? -??????? _ LocalTime -> 2017-02-08 18:39:31 Configuration -> ????. broker -> redis://localhost:6379// ????. loader -> celery.loaders.app.AppLoader ????. scheduler -> celery.beat.PersistentScheduler ????. db -> celerybeat-schedule ????. logfile -> [stderr]@%WARNING ????. maxinterval -> 5.00 minutes (300s)

    此時還差一步,就是還需要啟動一個worker,負責執行celery beat發起的任務

    啟動celery worker來執行任務

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ celery -A periodic_task worker ?? ?-------------- celery@Alexs-MacBook-Pro.local?v4.0.2 (latentcall) ---- **** ----- --- * ***? * -- Darwin-15.6.0-x86_64-i386-64bit 2017-02-08 18:42:08 -- * - **** --- - ** ---------- [config] - ** ---------- .> app:???????? tasks:0x104d420b8 - ** ---------- .> transport:?? redis://localhost:6379// - ** ---------- .> results:???? redis://localhost/ - *** --- * --- .> concurrency: 8 (prefork) -- ******* ---- .> task events: OFF (enable?-E to monitor tasks?in?this worker) --- ***** ----- ?-------------- [queues] ????????????????.> celery?????????? exchange=celery(direct) key=celery

      

    好啦,此時觀察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 $ celery -A periodic_task beat -s?/home/celery/var/run/celerybeat-schedule

    ?

    更復雜的定時配置  

    上面的定時任務比較簡單,只是每多少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 from?celery.schedules?import?crontab app.conf.beat_schedule?=?{ ????# Executes every Monday morning at 7:30 a.m. ????'add-every-monday-morning': { ????????'task':?'tasks.add', ????????'schedule': crontab(hour=7, minute=30, day_of_week=1), ????????'args': (16,?16), ????}, }

    上面的這條意思是每周1的早上7.30執行tasks.add任務

    還有更多定時配置方式如下:

    Example Meaning
    crontab() Execute every minute.
    crontab(minute=0,?hour=0) Execute daily at midnight.
    crontab(minute=0,?hour='*/3') Execute every three hours: midnight, 3am, 6am, 9am, noon, 3pm, 6pm, 9pm.
    crontab(minute=0,
    hour='0,3,6,9,12,15,18,21')
    Same as previous.
    crontab(minute='*/15') Execute every 15 minutes.
    crontab(day_of_week='sunday') Execute every minute (!) at Sundays.
    crontab(minute='*',
    hour='*',day_of_week='sun')
    Same as previous.
    crontab(minute='*/10',
    hour='3,17,22',day_of_week='thu,fri')
    Execute every ten minutes, but only between 3-4 am, 5-6 pm, and 10-11 pm on Thursdays or Fridays.
    crontab(minute=0,hour='*/2,*/3') Execute every even hour, and every hour divisible by three. This means: at every hour?except: 1am, 5am, 7am, 11am, 1pm, 5pm, 7pm, 11pm
    crontab(minute=0,?hour='*/5') 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).
    crontab(minute=0,?hour='*/3,8-17') Execute every hour divisible by 3, and every hour during office hours (8am-5pm).
    crontab(0,?0,day_of_month='2') Execute on the second day of every month.
    crontab(0,?0,
    day_of_month='2-30/3')
    Execute on every even numbered day.
    crontab(0,?0,
    day_of_month='1-7,15-21')
    Execute on the first and third weeks of the month.
    crontab(0,?0,day_of_month='11',
    month_of_year='5')
    Execute on the eleventh of May every year.
    crontab(0,?0,
    month_of_year='*/3')
    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 from?__future__?import?absolute_import, unicode_literals import?os from?celery?import?Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE',?'proj.settings') app?=?Celery('proj') # Using a string here means the worker don't have to serialize # the configuration object to child processes. # - namespace='CELERY' means all celery-related configuration keys #?? should have a `CELERY_` prefix. app.config_from_object('django.conf:settings', namespace='CELERY') # Load task modules from all registered Django app configs. app.autodiscover_tasks() @app.task(bind=True) def?debug_task(self): ????print('Request: {0!r}'.format(self.request))

    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 from?__future__?import?absolute_import, unicode_literals # This will make sure the app is always imported when # Django starts so that shared_task will use this app. from?.celery?import?app as celery_app __all__?=?['celery_app']

    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 from?__future__?import?absolute_import

    Then we set the default?DJANGO_SETTINGS_MODULE?environment variable for the?celery?command-line program:

    1 os.environ.setdefault('DJANGO_SETTINGS_MODULE',?'proj.settings')

    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 app?=?Celery('proj')

    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 app.config_from_object('django.conf:settings', namespace='CELERY')

    Next, a common practice for reusable apps is to define all tasks in a separate?tasks.pymodule, and Celery does have a way to? auto-discover these modules:

    1 app.autodiscover_tasks()

    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 -?app1/ ????-?tasks.py ????-?models.py -?app2/ ????-?tasks.py ????-?models.py

    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 # Create your tasks here from?__future__?import?absolute_import, unicode_literals from?celery?import?shared_task @shared_task def?add(x, y): ????return?x?+?y @shared_task def?mul(x, y): ????return?x?*?y @shared_task def?xsum(numbers): ????return?sum(numbers)

    ?

    ?

    在你的django views里調用celery task

    1 2 3 4 5 6 7 8 9 10 11 12 13 from?django.shortcuts?import?render,HttpResponse # Create your views here. from??bernard?import?tasks def?task_test(request): ????res?=?tasks.add.delay(228,24) ????print("start running task") ????print("async task res",res.get() ) ????return?HttpResponse('res %s'%res.get())

      

      

      

    五、在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?django_celery_beat?module to?INSTALLED_APPS?in your Django project’?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?django?scheduler:

    $ celery -A proj beat -l info -S django
  • Visit the Django-Admin interface to set up some periodic tasks.

  •   

    在admin頁面里,有3張表

    配置完長這樣

    ?

    ?

    此時啟動你的celery beat 和worker,會發現每隔2分鐘,beat會發起一個任務消息讓worker執行scp_task任務

    ?

    注意,經測試,每添加或修改一個任務,celery beat都需要重啟一次,要不然新的配置不會被celery beat進程讀到

    總結

    以上是生活随笔為你收集整理的python Celery 分布式任务队列快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 一级片免费观看视频 | 成人毛片在线免费观看 | 中文字幕超清在线免费观看 | 欧美性猛交乱大交xxxx | 一级色视频 | 国产av剧情一区二区三区 | 国产三级三级在线观看 | 日日不卡av | 欧美理论在线 | a∨色狠狠一区二区三区 | 综合久久一区二区 | 亚洲视频在线看 | 人妻91麻豆一区二区三区 | 天堂网在线最新版www中文网 | 中文一二三区 | 久久久久久国产精品免费免费 | 妇女一级片 | 国产资源第一页 | 野战少妇38p | 免费av网站在线播放 | 日本黄色一区二区 | 久久久国产精华液999999 | 能看av的网站 | 久久综合欧美 | 亚洲欧洲综合av | 日本黄色aaa | 美女又大又黄 | 亚洲美女网站 | 天天天干干干 | 国产精品乱码一区二区 | 97超碰中文 | 午夜精品网站 | 性视频网 | 欧美99视频 | 久久99精品久久久久久国产越南 | 精东av在线 | 亚洲最黄视频 | 日韩不卡在线 | 亚洲五月六月 | 99自拍偷拍 | 少妇无套内谢免费视频 | 一区二区三区在线播放视频 | 蜜桃成人在线视频 | av网站观看 | 91精品一区二区 | 一道本久在线中文字幕 | 污污视频网站在线免费观看 | 国产高清免费在线观看 | 日本欧美另类 | 精品久草 | 免费看黄网站在线 | 欧美乱人伦| 内裤摩擦1v1h| 欧洲金发美女大战黑人 | 91久久精品日日躁夜夜躁欧美 | 欧美日韩欧美 | 呦呦av| 成人国产精品一区 | 四虎午夜| 91免费视频黄 | 69亚洲精品久久久蜜桃小说 | 亚洲狠狠爱 | 久久久久久18 | 中国少妇做爰全过程毛片 | 成人在线激情网 | 中文字幕在线免费观看 | 欧美亚洲免费 | 台湾性生生活1 | 一本久道综合色婷婷五月 | 国产99久久久国产精品成人免费 | 国产精品一线二线 | av美女网站 | 亚洲AV无码乱码国产精品色欲 | 成人深夜福利在线观看 | 韩国三级做爰高潮 | 九色精品在线 | 久久久久久五月天 | 爱爱视频久久 | 久久这里只有精品国产 | 日韩国产在线播放 | 亚洲黄站 | 国产传媒第一页 | 大片av| 激情六月天婷婷 | 黄色av免费观看 | 年代下乡啪啪h文 | 小sao货cao死你| 亚洲精品视频久久久 | 日韩大片一区 | 国产在线播放一区 | 美女穴穴 | 欧美日本成人 | 久久久久看片 | 日韩不卡一区二区 | 一级黄色免费毛片 | 九七影院在线观看免费观看电视 | 美女网站在线看 | 欧美黄色视屏 | 人妻在线日韩免费视频 |