Python - Celery任务调用其他函数不起作用?

时间:2016-03-10 17:29:37

标签: python celery celery-task

我似乎无法在Celery计划任务中调用其他函数。主程序启动后,任务会在10秒后触发。使用rabbitmq作为经纪人。

我正在测试任务中的两个电话:

  • 使用标准记录器记录消息 - 任务:没有记录到日志文件
  • 将python的zen下载到磁盘上的txt文件 - 任务:没有下载文件

主程序:

import mytasks
import utilities
from time import sleep

if __name__ == '__main__':
    utilities.start_message("log: program started")
    print("program started")
    mytasks.test_message.apply_async(countdown=10)
    sleep(3000)

任务模块:

from celery import Celery
import utilities

celery = Celery(__name__,
                broker='amqp://guest@localhost//',
                backend='amqp://guest@localhost//')


@celery.task
def test_message():
    utilities.display_message("hello")
    utilities.acquire_zen()

实用程序模块,由任务调用:

import logging
import urllib.request

logging.basicConfig(filename="message.log",
                    filemode="w",
                    level=logging.DEBUG,
                    format='%(asctime)s.%(msecs)d %(levelname)s %(module)s - %(funcName)s: %(message)s',
                    datefmt="%Y-%m-%d %H:%M:%S")


def start_message(message):
    logging.info(message)


def display_message(message):
    final_message = "test message: " + message
    logging.info(final_message)
    return ("terminal message" + message)


def acquire_zen():
    url = "https://gist.githubusercontent.com/baccenfutter/61c5eb9c482df4200611/raw/5acfab2350116f076f47b44cb991950b3e1db7c8/zen-of-python.txt"
    fo = open('python_zen.txt', 'w')
    with urllib.request.urlopen(url) as u:
        s = u.read()
    fo.write(str(s))
    fo.close()

芹菜终端输出:

[2016-03-11 03:46:56,956: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2016-03-11 03:46:56,967: INFO/MainProcess] mingle: searching for neighbors
[2016-03-11 03:46:57,978: INFO/MainProcess] mingle: all alone
[2016-03-11 03:46:57,993: WARNING/MainProcess] XX@YYYYY.local ready.
[2016-03-11 04:06:25,801: INFO/MainProcess] Received task: mytasks.test_message[cef363cb-cef6-4b2a-b2a0-7b06f05e933a] eta:[2016-03-10 17:06:35.789261+00:00]
[2016-03-11 04:06:35,806: INFO/Worker-4] test message: hello
[2016-03-11 04:06:35,858: INFO/MainProcess] Task mytasks.test_message[cef363cb-cef6-4b2a-b2a0-7b06f05e933a] succeeded in 0.051920230966061354s: None

现在,芹菜输出是由return final_message声明引起的。该消息实际上未添加到日志中。也没有下载python_zen.txt。然而,如果我在没有在Celery中安排它们的情况下调用这些函数,它们就能正常工作。

我对芹菜做错了什么?

0 个答案:

没有答案