芹菜周期性任务不访问模块变量

2024-10-03 17:19:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个配置良好的芹菜和django一起工作。 在发出post\u save信号时,我使用一个任务向一个集合发送一条新记录 使用另一个周期性的任务,我试着使用这一组。你知道吗

from __future__ import absolute_import, unicode_literals
from celery import shared_task

class Data():
    def __init__(self):
        self.slotshandler = set()

global data
data = Data()

@shared_task
def ProcessMailSending(): #This is a periodic task, running every 30 seconds
    global data #This variable is always empty here
    while slotshandler:
        slot_instance = slotshandler.pop()
        print("sending mail for slot } to {} by mail {}".format(slot_instance .id,slot_instance .user,slot_instance .user_mail))

@shared_task
def UpdateSlotHandler(new_slot): #This is called by save_post django signal
    global data
    data.slotshandler.add(new_slot) #filling the set at each new record

问题是这个任务没有看到我新添加的时间段。 请注意,这个django应用程序运行在一个微型服务上,用于向用户发送提醒邮件。你知道吗


Tags: djangoinstanceimportnewtaskdataisdef
1条回答
网友
1楼 · 发布于 2024-10-03 17:19:43

不同的芹菜任务产生不同的进程,这些进程不共享对内存的访问。i、 e.在这些进程之间,您的全局进程不是持久的。当第一个任务完成时,与它的进程相关联的所有内存都将被刷新。第二个任务是在内存中创建一组全新的对象,包括全局变量。你知道吗

您确实需要将数据保存在更持久的对象中,可以是db或内存缓存(例如Memcache)。你知道吗

相关问题 更多 >