芹菜任务排队

时间:2018-03-07 04:17:28

标签: flask celery scheduled-tasks

我创建了一个烧瓶应用程序,它包含2个芹菜任务。

任务1:通过流程生成文件

任务2:通过电子邮件发送生成的文件

与任务2相比,通常任务1需要更多时间。我想执行任务1然后执行任务2.但问题是在芹菜内部同时开始执行。

如何解决此问题。

@celery.task(name='celery_example.process')
def process(a,b,c,d,e,f):
    command='rnx2rtkp -p '+a+' -f '+b+' -m '+c+' -n -o oout.pos '+d+' '+e+' '+f
    os.system(command)
    return 'Successfully created POS file'

@celery.task(name='celery_example.emailfile')
def emailfile(recipientemail):  
    email_user = ''
    email_password = ''
    subject = 'subject'
    msg = MIMEMultipart()
    msg['From'] = email_user
    msg['To'] = recipientemail
    msg['Subject'] = subject
    body = 'This is your Post-Processed position file'
    msg.attach(MIMEText(body,'plain'))
    filename='oout.pos'
    attachment  =open(filename,'rb')
    part = MIMEBase('application','octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition',"attachment; filename= "+filename)
    msg.attach(part)
    text = msg.as_string()
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.starttls()
    server.login(email_user,email_password)
    server.sendmail(email_user,recipientemail,text)
    server.quit()
    return 'Email has been successfully sent'   

这是app.route

@app.route('/pp.php', methods=['GET', 'POST'])
def pp():
    pp = My1Form()
    target = os.path.join(APP_ROOT)
    print(target)

    for fileBase in request.files.getlist("fileBase"):
        print(fileBase)
        filename = fileBase.filename
        destination = "/".join([target, filename])
        print(destination)
        fileBase.save(destination)

    for fileObsRover in request.files.getlist("fileObsRover"):
        print(fileObsRover)
        filename = fileObsRover.filename
        destination = "/".join([target, filename])
        print(destination)
        fileObsRover.save(destination)

    for fileNavRover in request.files.getlist("fileNavRover"):
        print(fileNavRover)
        filename = fileNavRover.filename
        destination = "/".join([target, filename])
        print(destination)
        fileNavRover.save(destination)

        a=fileObsRover.filename
        b=fileBase.filename
        c=fileNavRover.filename
        elevation=pp.ema.data
        Freq=pp.frq.data
        posMode=pp.pmode.data
        emailAdd=pp.email.data

        process.delay(posMode,Freq,elevation,a,b,c)
        emailfile.delay(emailAdd)

        return render_template('results.html', email=pp.email.data, Name=pp.Name.data, ema=elevation, frq=Freq, pmode=posMode, fileBase=a)
    return render_template('pp.php', pp=pp)

1 个答案:

答案 0 :(得分:1)

目前,您的代码执行以下操作:

# schedule process to run asynchronously
process.delay(posMode,Freq,elevation,a,b,c)

# schedule emailfile to run asynchronously
emailfile.delay(emailAdd)

这些都将立即被工人接收并执行。您已提供无法通知celery emailfile应等到processfile完成

相反,你应该:

  • 更改emailfile的签名以包含另一个参数,该参数将成为成功processfile调用的输出;然后
  • 使用processfile致电link

例如:

deferred = processfile.apply_async(
    (posMode,Freq,elevation,a,b,c), 
    link=emailfile.s()) 
deferred.get()

使用link的替代方法,但在这种情况下语义相同,将使用chain