Home > database >  How do I establish a minimum time for thread completion?
How do I establish a minimum time for thread completion?

Time:02-01

I'm using multithreading to download videos from a website, however sometimes if the video is too small, the program starts another thread too fast, and the server blocks my request.

I don't want to use time.sleep because that will slow down requests that are not required to be slowed down.

So basically I need a command to establish a minimum execution time, like.

Pseuco code

minimum time = 20 seconds
If thread ended has been completed faster than minimum time:
     wait until minimum time has been reached

CodePudding user response:

Without trying something overly complex to achieve this, you could a) start a timer when the thread starts b) when the thread finishes, sleep for only the remainder of the time needed to reach the minimum duration:

start = ...
<threading code>
duration = now() - start
if duration < minimum:
    time.sleep(minimum - duration)

Which is basically slightly less-pseudo pseudo code of what you've got in your question.

  •  Tags:  
  • Related