首页 > 解决方案 > 为什么 Python Celery 任务被调用了几次,尽管被调用了一次

问题描述

有这个代码。调用 Celery 任务,它是一个 Python 脚本。想知道为什么我在执行时收到 3 封电子邮件。

test() 被调用了 3 次??????

 @client.task()
 def test(id):
      call(["python", "/script.py", "-t",
            "test", "-s", "test", "-o", "real", "-i", "{:s}".format(id)])
      message = Mail(
        from_email='from',
        to_emails='to',
        subject="subject {:s}".format(id),
        html_content='<strong>text</strong>')
      try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
      except Exception as e:
        print(e.message)



@app.route("/test", methods=['GET'])
@oidc.accept_token(True)
def testbyID():
  id = request.args.get('id')
  result = test.apply_async(args=[id], countdown=60)
  return result.id

处理完任务后,如何更正它以仅收到一封电子邮件?

更新1:

将其更改为

task.delay() 并立即执行任务并仅收到一封电子邮件

仍然想知道如何让它与 1 封电子邮件异步执行?

标签: pythoncelery

解决方案


推荐阅读