S
S
Sanya Hihi Haha2021-11-18 10:34:45
Django
Sanya Hihi Haha, 2021-11-18 10:34:45

How to organize micro slips in a celery task?

Good day.
Actually the question is this.
I wrote a bot on aiogram + backend on django.
The bot performs mailing via telegram channels.

How it works:
In the bot, we receive a request to start the mailing list -> we process the request on the back and start the mailing list (of course, celery was chosen for this). The account that receives the command to send it starts to do it, but between each message I need to make an interval of 3 seconds and between every 100 messages 1000 seconds. (there may be several such parallel mailings)

the code looks something like this:

While True:
      for g in groups[:100]:
          send_message(g, msg)
          time.sleep(3)
      time.sleep(1000)


From the answers right there on the toaster, I saw that celery does not allow the use of sleep (in any case, then other tasks will be blocked).

In theory, I can create a task using celery beat with an interval of 1000 seconds, but 3 seconds of sleep between each message haunts me, because I need some kind of competition between several mailings so that the conditional 2-7 seconds are observed for each of the running mailings.

I would be very grateful for an explanation.

PS maybe I'm looking in the wrong direction at all, or somewhere I misunderstood something

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
marazmiki, 2021-11-18
@ValarMayar

Move sending one message to one chat into a separate task. And in the second task, inside the loop, call these tasks with an ETA

A
alternativshik, 2021-11-18
@alternativshik

some kind of global bucket is needed, if 10 mailings are launched at the same time, then there will not be 3 seconds of sleep between requests, even if you put them in each task. And on the basis of this global some kind of counter, you need to steer all mailings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question