0

Python noob here , I apologize in advance if this is a stupid question.

So , I wrote this code

import subprocess
import os, signal
import time
timeout = time.time() + 60*5
count = 0

while True:
    print("running")
    if time.time() > timeout:
        break
    
    speedtest = subprocess.Popen("C:\Program Files\Speedtest\Speedtest.exe")
    time.sleep(30.0)
    speedtest.terminate()

I`m trying to open speedtest.exe, keep it open for 30 seconds and close it. I want to keep repeating this for 5 minutes.

But when the process closes for the first time and tries to reopen , I get an error saying another instance is already running.

How can I fix this ?

2
  • 1
    Try sleeping a little bit right after you call .terminate() Commented May 26, 2021 at 3:30
  • @12944qwerty It worked , thank you for helping Commented May 26, 2021 at 3:39

1 Answer 1

1

You will have to sleep a little bit after terminating the executable. This is because terminating takes a bit of time, and unfortunately, the terminate method doesn't take all that time when compiling. It goes directly from mid-termination to starting the exe again.
By waiting, you're waiting for the executable to stop and then start the application. You will have to mess with the timings a bit though since there will generally be a random amount of time for termination.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.