问题
I'm working with celery that I encountered a problem.
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) 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")
and finally I used celeryd and 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)
回答1:
It looks like your function is stuck on first one, as it is always in while loop.
来源:https://stackoverflow.com/questions/48648011/how-to-execute-two-tasks-at-same-time