celery安裝配置(Windows)

  1. 安裝RabbitMQ,會提示安裝Erlang,按照提示即可。
  2. 確認(rèn)RabbitMQ是否安裝成功。
    a. 啟動RabbitMQ Service
    b. 啟動RabbitMQ Command Prompt (sbin dir),執(zhí)行:rabbitmqctl status。報錯如下:
    Status of node rabbit@DESKTOP-P6AC70L ...
    Error: unable to perform an operation on node 'rabbit@DESKTOP-P6AC70L'. Please see diagnostics information and suggestions below.
    
    Most common reasons for this are:
    
     * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)
     * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)
     * Target node is not running
    
    In addition to the diagnostics info below:
    
     * See the CLI, clustering and networking guides on http://rabbitmq.com/documentation.html to learn more
     * Consult server logs on node rabbit@DESKTOP-P6AC70L
    
    DIAGNOSTICS
    ===========
    
    attempted to contact: ['rabbit@DESKTOP-P6AC70L']
    
    rabbit@DESKTOP-P6AC70L:
      * connected to epmd (port 4369) on DESKTOP-P6AC70L
      * epmd reports node 'rabbit' uses port 25672 for inter-node and CLI tool traffic
      * TCP connection succeeded but Erlang distribution failed
    
      * Authentication failed (rejected by the remote node), please check the Erlang cookie
    
    
    Current node details:
     * node name: 'rabbitmqcli60@DESKTOP-P6AC70L'
     * effective user's home directory: C:\Users\junwe
     * Erlang cookie hash: aXFZrVZ2A9gF0kkNeMhRXA==
    
    c. 搜索.erlang.cookie(應(yīng)該有兩個),用任意一個替換另外一個保證內(nèi)容一致。
    d. 重裝RabbitMQ Service - 運行RabbitMQ Service - (re)install
    e. 重復(fù)步驟a、b,可以看到RabbitMQ Service已經(jīng)運行起來。
    f. 啟動RabbitMQ Command Prompt (sbin dir),執(zhí)行:rabbitmq-plugins enable rabbitmq_management
    g. 打開瀏覽器,輸入http://127.0.0.1:15672,成功跳轉(zhuǎn)至RabbitMQ管理界面。(不運行步驟f,頁面無法訪問)
  3. 安裝celery。在python環(huán)境下執(zhí)行:pip install celery
  4. 創(chuàng)建tasks.py:
    from celery import Celery
    import time
    
    app = Celery('tasks', backend='amqp', broker='amqp://')
    
    @app.task
    def add(x, y):
      time.sleep(5)
      return x + y
    
  5. 切到tasks.py目錄下運行:celery -A tasks worker --loglevel=info
    提示錯誤:
      -------------- celery@iZev0nc46ircftZ v4.1.0 (latentcall)
      ---- **** -----
      --- * ***  * -- Windows-10-10.0.14393-SP0 2018-04-19 21:24:48
      -- * - **** ---
      - ** ---------- [config]
      - ** ---------- .> app:         tasks:0x213343e6630
      - ** ---------- .> transport:   amqp://guest:**@localhost:5672//
      - ** ---------- .> results:     disabled://
      - *** --- * --- .> concurrency: 2 (prefork)
      -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
      --- ***** -----
       -------------- [queues]
                    .> celery           exchange=celery(direct) key=celery
      
      
      [tasks]
        . tasks.add
      
      [2018-04-19 21:24:48,634: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [WinError 10042] 在 getsockopt 或 setsockopt 調(diào)用中指定的一個未知的、無效的或不受支持的選項或?qū)哟巍?
      Trying again in 2.00 seconds...
      
      [2018-04-19 21:24:49,322: INFO/SpawnPoolWorker-2] child process 2956 calling self.run()
      [2018-04-19 21:24:49,353: INFO/SpawnPoolWorker-1] child process 5768 calling self.run()
      [2018-04-19 21:24:50,697: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [WinError 10042] 在 getsockopt 或 setsockopt 調(diào)用中指定的一個未知的、無效的或不受支持的選項或?qū)哟巍?
      Trying again in 4.00 seconds...
    
    Google后發(fā)現(xiàn)是有個bug,由于amqp的版本太低沒有被修復(fù):https://github.com/celery/py-amqp/pull/136。由于我是anaconda環(huán)境,運行conda update --all把所有的package都更新了一下。再運行celery -A tasks worker --loglevel=info就可以了。
  6. 在調(diào)用端運行如下代碼:
    >>> from tasks import add
    >>> res = add.delay(4,4)
    >>> res.ready()
    False
    >>> res.ready()
    True
    >>> res.result
    8
    >>>
    
    切到celery的環(huán)境下看一下,發(fā)現(xiàn)報錯了:
        The AMQP result backend is scheduled for deprecation in     version 4.0 and removal in version v5.0.     Please use RPC backend or a persistent backend.
    
      alternative='Please use RPC backend or a persistent backend.')
    [2018-04-20 16:27:26,581: INFO/SpawnPoolWorker-1] child process 4104 calling self.run()
    [2018-04-20 16:27:26,596: WARNING/SpawnPoolWorker-1] c:\programdata\anaconda3\lib\site-packages\celery\backends\amqp.py:68: CPendingDeprecationWarning:
        The AMQP result backend is scheduled for deprecation in     version 4.0 and removal in version v5.0.     Please use RPC backend or a persistent backend.
    
      alternative='Please use RPC backend or a persistent backend.')
    [2018-04-20 16:27:26,800: INFO/MainProcess] mingle: all alone
    [2018-04-20 16:27:26,846: INFO/MainProcess] celery@iZ15r2bhksa4s0Z ready.
    [2018-04-20 16:28:23,878: INFO/MainProcess] Received task: tasks.add[0a988088-95da-44a1-8c04-7c4581ad6f25]
    [2018-04-20 16:28:23,956: ERROR/MainProcess] Task handler raised error: ValueError('not enough values to unpack (expected 3, got 0)',)
    Traceback (most recent call last):
      File "c:\programdata\anaconda3\lib\site-packages\billiard\pool.py", line 358, in workloop
        result = (True, prepare_result(fun(*args, **kwargs)))
      File "c:\programdata\anaconda3\lib\site-packages\celery\app\trace.py", line 525, in _fast_trace_task
        tasks, accept, hostname = _loc
    ValueError: not enough values to unpack (expected 3, got 0)
    
    Google了一下發(fā)現(xiàn)celery 4.0版本后就放棄了對Windows的支持:https://github.com/celery/celery/issues/4178。找到一個workaround:
    1. pip install eventlet
    2. celery -A tasks worker --pool=eventlet
  7. 使用redis作為backend
    a. pip install redis
    b. 代碼改為:app = Celery('tasks', broker='redis://127.0.0.1:6379/0', backend='redis://127.0.0.1:6379/0')
    c. 安裝/啟動redis server
    d. 重復(fù)步驟5、6
  8. 在兩臺機器上進行測試
    a. 修改tasks.py中的ip至rabbitmq/redis所在機器的公網(wǎng)網(wǎng)址
    b. 拷貝tasks.py到另外一個機器
    c. 在另外一個機器上執(zhí)行pip install celery和pip install redis
    d. 重復(fù)步驟6
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容