R
R
Rotyin2021-11-04 23:01:47
Python
Rotyin, 2021-11-04 23:01:47

How can you async and speed up your code?

for i in range(1,string[0]+1):
        photo_url = f"Tinder/{i}/1.jpg"
        try:
            num = compare_faces(photo_url, "s.jpg")
            if num == True:
                file = open(f"Tinder/{i}/text.txt","r",encoding = "utf-8")
                caption = ''
                for i in file:
                    caption = caption + i
                file.close()
                files = open(photo_url, "rb")
                bot.send_photo(message.chat.id,files)
                bot.send_message(message.chat.id,caption)
        except Exception as e:
            print(e)

the delay occurs on this line: A one-way function that returns True or False. How can this be accelerated?
num = compare_faces(photo_url, "s.jpg")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Mirilaczvili, 2021-11-05
@Rotyin

  1. Of course, the calculation of the vector face_encodingsfrom the image of the standard suggests itself only once, taking it out before the cycle.
  2. Both calls face_recognition.load_image_filecan be made asynchronous with await on each.
    Similarly, any other I / O operations with files and functions bot.send_photoand bot.send_messageseparately.
  3. The face_recognition.compare_facestask is computational, so it remains to create several processes according to the number of processor cores, bypassing the queue, and exchange the results (calculated hashes) through the pipe.

K
kirillinyakin, 2021-11-05
@kirillinyakin

You can use the multiprocessing module, including using a pool of processes so as not to kill the server))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question