向多个工作人员分发推送通知

时间:2014-06-17 22:09:16

标签: mysql django celery django-celery

假设您有数百万个Android GCM设备密钥,并且您希望在管理脚本中发送它们。这个脚本需要花费大量时间才能完成,因为它将DB中的密钥作为队列进行处理。

问题:您如何更快地实现此目标?你如何并行发送这些通知?你如何得到近乎实时的推送通知?

一种解决方案是实例化一个X数量的芹菜工人,其中每个工人负责一个偏移Y,它开始从MySQL获取。 例如:

Worker 1: starts at offset 0,
Worker 2: starts at offset 10,000,
Worker 3: starts at offset 20,000,
Worker 4: starts at offset 30,000,
Worker 5: starts at offset 40,000,

Worker 1: Restarts at offset 50,000,
Worker 2: Restarts at offset 60,000,

...等

这是一个可行的解决方案吗?

1 个答案:

答案 0 :(得分:0)

Celery group创建任务列表。另外,因为您必须从Android模型中检索所有记录,所以最好创建单独的芹菜任务,这将在后台执行:

@shared_task
def push_notification(offset, limit):
   for android in Android.objects.all()[offset:offset+limit]:
       pass

@shared_task
def push_notification_to_all():
   count = Android.objects.all().count()
   limit = 100
   group(push_notification.s(offset, limit) for offset in range(0, count, limit)()

push_notification_to_all.delay()

而不是发送

相关问题