如何同时执行两个任务 [英] How to execute two tasks at same time

查看:90
本文介绍了如何同时执行两个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理芹菜时遇到问题.

I'm working with celery that I encountered a problem.

我有两个功能:1)激活程序后,此功能将被激活,并且将无限运行:

I have two functions: 1) This function will be activated when the program is activated, and it will work infinitely:

from celery.signals import worker_ready    

@worker_ready.connect()
def message_poll_start(sender=None, headers=None, body=None, **kwargs):
    while True:
        time.sleep(2)
        print("hello")

2)此功能每十秒钟激活一次,并在txt文件中写入一个日期:

2) This function will be activated every ten seconds and write a date in a txt file:

@periodic_task(run_every=timedelta(seconds=10))
def last_record_time_check():
    file_text = open('file.txt', 'a')
    file_text.write("===========" + str(datetime.datetime.now()) +
                    " =============== \n\n")

最后我使用了芹菜 celerybeat

第一个功能没有问题,但是第二个功能根本不起作用.

The first function works without problems, but the second function does not work at all.

[2018-02-06 16:43:17,802: INFO/MainProcess] beat: Starting...
[2018-02-06 16:43:27,947: INFO/MainProcess] Scheduler: Sending due task base.tasks.last_record_time_check (base.tasks.last_record_time_check)
[2018-02-06 16:43:37,925: INFO/MainProcess] Scheduler: Sending due task base.tasks.last_record_time_check (base.tasks.last_record_time_check)
[2018-02-06 16:43:47,926: INFO/MainProcess] Scheduler: Sending due task base.tasks.last_record_time_check (base.tasks.last_record_time_check)

推荐答案

您的函数似乎停留在第一个函数上,因为它始终处于while循环中.

It looks like your function is stuck on first one, as it is always in while loop.

这篇关于如何同时执行两个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆