工作人员从Redis队列消耗任务后立即退出

时间:2019-07-11 19:52:21

标签: python docker sqlalchemy celery python-billiard

我试图与Celery异步运行任务。我已经在互联网上进行了广泛搜索,但还没有完全在网上找到我的错误。我正在从Docker容器中运行celery 4.3。

尽管错误仍然存​​在,但我已经尝试升级到4.4版本的候选版本。我以为这个pull request可以解决我的问题,但是没有解决。

我也怀疑sql-alchemy可能是问题所在,但我不确定为什么。我还有其他功能可以在异步调用时起作用,但是由于某种原因,当我使用这种格式时,我的代码无法运行。

我还尝试设置无效的任务选项bind=True

import sqlalchemy as sa
import pandas as pd

class MyTask(Task):

    """
    Custom extension of celery.Task that sends email alerts when task failure occurs.
    """

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        # Send error email.
        now = dt.datetime.now()
        to = ["it@guy.com"]
        subject = f"DSJOBS Error - {now:}!"
        template = JINJA2_ENVIRONMENT.get_template("email/error.html")
        text = template.render(
            args=args,
            einfo=einfo,
            exc=exc,
            kwargs=kwargs,
            task_id=task_id,
            title=str(type(einfo)),
        )
        send_email(to, subject, text)
        # Perform inherited actions on failure.
        super().on_failure(exc, task_id, args, kwargs, einfo)


@APP.task(base=MyTask, 
    name="dsjobs.tasks.get_keywords", 
    time_limit=12 * 3_600,)
def get_keywords(date):
  engine = sa.create_engine(**db_creds_here)
  df = pd.DataFrame(engine.query(f'select * from sometable where date = "{date}"'))
  #[do some work on df]
  return df

当我从get_keywords(d1)内像普通函数celery shell那样调用函数时,我的代码运行良好,并且得到了预期的结果。

问题出在我尝试使用异步任务的基本芹菜功能时。当我使用get_keywords.delay(d1)时,任务将提交到我的Redis队列,然后我的工作人员返回此错误:

[2019-07-11 12:28:32,839: ERROR/MainProcess] Error on stopping Pool: TypeError("__init__() missing 1 required positional argument: 'name'")
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/celery/worker/worker.py", line 205, in start
    self.blueprint.start(self)
  File "/opt/conda/lib/python3.7/site-packages/celery/bootsteps.py", line 119, in start
    step.start(parent)
  File "/opt/conda/lib/python3.7/site-packages/celery/bootsteps.py", line 369, in start
    return self.obj.start()
  File "/opt/conda/lib/python3.7/site-packages/celery/worker/consumer/consumer.py", line 318, in start
    blueprint.start(self)
  File "/opt/conda/lib/python3.7/site-packages/celery/bootsteps.py", line 119, in start
    step.start(parent)
  File "/opt/conda/lib/python3.7/site-packages/celery/bootsteps.py", line 369, in start
    return self.obj.start()
  File "/opt/conda/lib/python3.7/site-packages/celery/worker/consumer/consumer.py", line 318, in start
    blueprint.start(self)
  File "/opt/conda/lib/python3.7/site-packages/celery/bootsteps.py", line 119, in start
    step.start(parent)
  File "/opt/conda/lib/python3.7/site-packages/celery/worker/consumer/consumer.py", line 596, in start
    c.loop(*c.loop_args())
  File "/opt/conda/lib/python3.7/site-packages/celery/worker/loops.py", line 91, in asynloop
    next(loop)
  File "/opt/conda/lib/python3.7/site-packages/kombu/asynchronous/hub.py", line 362, in create_loop
    cb(*cbargs)
  File "/opt/conda/lib/python3.7/site-packages/celery/concurrency/asynpool.py", line 300, in on_result_readable
    next(it)
  File "/opt/conda/lib/python3.7/site-packages/celery/concurrency/asynpool.py", line 281, in _recv_message
    message = load(bufv)
TypeError: __init__() missing 1 required positional argument: 'name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/celery/bootsteps.py", line 151, in send_all
    fun(parent, *args)
  File "/opt/conda/lib/python3.7/site-packages/celery/bootsteps.py", line 373, in stop
    return self.obj.stop()
  File "/opt/conda/lib/python3.7/site-packages/celery/concurrency/base.py", line 122, in stop
    self.on_stop()
  File "/opt/conda/lib/python3.7/site-packages/celery/concurrency/prefork.py", line 145, in on_stop
    self._pool.join()
  File "/opt/conda/lib/python3.7/site-packages/billiard/pool.py", line 1581, in join
    stop_if_not_current(self._result_handler)
  File "/opt/conda/lib/python3.7/site-packages/billiard/pool.py", line 143, in stop_if_not_current
    thread.stop(timeout)
  File "/opt/conda/lib/python3.7/site-packages/billiard/pool.py", line 500, in stop
    self.on_stop_not_started()
  File "/opt/conda/lib/python3.7/site-packages/celery/concurrency/asynpool.py", line 336, in on_stop_not_started
    on_state_change,
  File "/opt/conda/lib/python3.7/site-packages/celery/concurrency/asynpool.py", line 360, in _flush_outqueue
    task = reader.recv()
  File "/opt/conda/lib/python3.7/site-packages/billiard/connection.py", line 281, in recv
    return ForkingPickler.loadbuf(buf)
  File "/opt/conda/lib/python3.7/site-packages/billiard/reduction.py", line 61, in loadbuf
    return cls.loads(buf.getbuffer())
TypeError: __init__() missing 1 required positional argument: 'name'

非常感谢您的帮助!

0 个答案:

没有答案