Celery worker不是从Python启动的

时间:2018-08-24 18:59:53

标签: python rabbitmq celery

我们在Ubuntu 14.04上使用Django,Celery和Rabbitmq设置了Python 3.6.1。现在,我正在使用Django调试服务器(对于dev和Apache无法正常工作)。我当前的问题是芹菜工人从Python启动后立即死亡-进程显示为已失效。如果我在终端窗口中使用相同的命令,则在队列中有一个等待的工人的情况下,将创建该工人并接任务。

这是命令:

celery worker --app=myapp --loglevel=info --concurrency=1 --maxtasksperchild=20 -n celery_1 -Q celery

无论设置哪个队列,都具有相同的功能。

在终端中,我们看到输出myapp.settings - INFO - Loading...,后面是描述队列并列出任务的输出。从Python运行时,我们最后看到的是Loading...

在代码中,我们进行了检查以确保我们没有以root用户身份运行celery命令。

这些是我们settings.py文件中的Celery设置:

CELERY_ACCEPT_CONTENT = ['json','pickle']
CELERY_TASK_SERIALIZER = 'pickle'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_IMPORTS = ('api.tasks',)
CELERYD_PREFETCH_MULTIPLIER = 1
CELERYD_CONCURRENCY = 1
BROKER_POOL_LIMIT = 120  # Note: I tried this set to None but it didn't seem to make any difference
CELERYD_LOG_COLOR = False
CELERY_LOG_FORMAT = '%)asctime)s - $(processName)s - %(levelname)s - %(message)s'
CELERYD_HIJACK_ROOT_LOGGER = False
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(psconf.BASE_DIR, 'myapp_static/')
BROKER_URL = psconf.MQ_URI
CELERY_RESULT_BACKEND = 'rpc'
CELERY_RESULT_PERSISTENT = True
CELERY_ROUTES = {}
for entry in os.scandir(psconf.PLUGIN_PATH):
    if not entry.is_dir() or entry.name == '__pycache__':
        continue
    plugin_dir = entry.name
    settings_file = f'{plugin_dir}.settings'
    try:
        plugin_tasks = importlib.import_module(settings_file)
        queue_name = plugin_tasks.QUEUENAME
    except ModuleNotFoundError as e:
        logging.warning(e)
    except AttributeError:
        logging.debug(f'The plugin {plugin_dir} will use the general worker queue.')
    else:
        CELERY_ROUTES[f'{plugin_dir}.tasks.run'] = {'queue': queue_name}
        logging.debug(f'The plugin {plugin_dir} will use the {queue_name} queue.')

以下是启动工作人员的部分:

    class CeleryWorker(BackgroundProcess):
      def __init__(self, n, q):
        self.name = n
        self.worker_queue = q        
        cmd = f'celery worker --app=myapp --loglevel=info --concurrency=1 --maxtasksperchild=20 -n {self.name" -Q {self.worker_queue}'
        super().__init__(cmd, cwd=str(psconf.BASE_DIR))

    class BackgroundProcess(subprocess.Popen):
      def __init__(self, args, **kwargs):
        super().__init__(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, **kwargs)

任何有关如何从Python进行工作的建议都值得赞赏。我是Rabbitmq / Celery的新手。

1 个答案:

答案 0 :(得分:0)

以防万一其他人需要这个...事实证明,问题在于启动整个应用程序的shell脚本现在正在使用sudo启动,即使我以为我正在检查,我们也不会用sudo启动芹菜工作者,我错过了一些东西,我们正尝试以root用户身份启动。那是禁忌。现在,我明确使用'sudo -u',并且工作程序正常启动。