1

Here is part of my code, db.users_vi() is a list file. When the program comes to def viber_not, it starts working very slow, it send 1 message per 30 sec or even slower. How can I make it work faster, and why it's so slow?

def viber_not():

   users = db.users_vi()

   text = random.choice(texts)

   for k in users:
       try:
           viber.send_messages(k[1], [TextMessage(text=text)])
       except:
           pass
Nikolas
  • 77
  • 9
  • Have you tried multithreading? – João Areias Sep 22 '20 at 18:28
  • No, I'm not. Can you please send me details about it? Where I can find information about multithreading – Nikolas Sep 22 '20 at 18:36
  • API calls on single-threaded applications will be very limited by the network latency since you are always waiting for the response. One way of solving this is making use of multithreading, in which you can send more submissions while waiting for the response of the others, here is a pretty good post about that https://creativedata.stream/multi-threading-api-requests-in-python/ – João Areias Sep 22 '20 at 18:37
  • Thank you, I'll try – Nikolas Sep 22 '20 at 18:47

1 Answers1

0

Try to serialize data requested from DB. As most of them returns "cursor", not data. For some of them wrapping in list is enough, but look to documentation of that you use.

users = list(db.users_vi())

wowkin2
  • 5,895
  • 5
  • 23
  • 66