-1

My first solution:

while True:
    doLongProcess()
    # other statements
    exitCondition()

This solution was running to much slow. So I tried below solution:

flag = 0
while True:

if flag == 5:
    doLongProcess()
    flag = 0

# other statements
flag += 1
exitCondition()

Instead of doing doLongProcess() every time I make it 1:5 ratio.

By applying this solution, execution speed speed up but at "every doLongProcess()" it slows down.

So my question is, Will thread programming help me in this scenario or not? If yes can you guide me a little?

Edit: doLongProcess() is processing image. If I tell you in short,

1.Load image from local storage

2.face_recognition library will find face location on that image

3.if face found: it will compare with previous data

4.Returns result

Aayush Scet
  • 37
  • 1
  • 6
  • Depends on the type of workload. With mostly I/O it will, with mostly CPU it won't. – Klaus D. Jul 06 '21 at 09:22
  • is the long process I/O bound or CPU bound? – Chris Doyle Jul 06 '21 at 09:22
  • Does this answer your question? [What are the differences between the threading and multiprocessing modules?](https://stackoverflow.com/questions/18114285/what-are-the-differences-between-the-threading-and-multiprocessing-modules) – buran Jul 06 '21 at 09:26
  • 1
    Sounds like that will be CPU bound so you proabably want multiprocessing instaed of multithreading – Chris Doyle Jul 06 '21 at 09:32

1 Answers1

1

the global idea of thrading is to make your computer run the code in a much smoother way... it may go faster but it depends on what you need to do (if the cpu is already at 100% it won't)

I think in your case it will prevent the programm to kinda freeze every 5 loops because of the big function.

if you wanna try threading it, here is an exemple of how to use threads with python

import threading
import time

class MyThread (threading.Thread):
    def __init__(self, stopAt):
        threading.Thread.__init__(self) # init (master call)
        self.stopAt = stopAt

# run function of the thread (executed when calling thread.start)
    def run(self):
        # your thread code
        for i in range(0, self.stopAt):
            print("thread ", i)
            time.sleep(0.1)


m = MyThread(10)
m.start()
time.sleep(0.05)

# your main program // goes in parallel with the thread
for i in range(10):
    print("programme ", i)
    time.sleep(0.1)

good luck with your project!

Ren
  • 157
  • 12