0

I am trying to run .exe files from the below locations

Code:

if __name__ == '__main__':
job1 = threading.Thread(target='C:\\Users\\XYZ\\PycharmProjects\\Task1\\dist\\Multiof2.exe')
job2 = threading.Thread(target='C:\\Users\\XYZ\\PycharmProjects\\Task2\\dist\\Multiof5.exe')

job1.start()
job2.start()

job1.join()
job2.join()

Error:

TypeError: 'str' object is not callable

I know that we can call .exe files using import subprocess but subclass.call() will start to execute only 1 file at a time, upon completion of First .exe file, it will start executing the Second .exe file. I need to run one or more .exe files at the same time and the .exe file which completes the execution First will prints(my .exe files will print after completion of its task).

multithread will support only callable functions or .exe files also?

If not support by multithread, then suggest to me how to run multiple .exe files.

I also tried import multiprocess and used multiprocess.process() to run .exe files but not worked.

  • Indent your code properly. – user202729 Feb 16 '21 at 15:08
  • Mostly a duplicate of [python - Non blocking subprocess.call - Stack Overflow](https://stackoverflow.com/questions/16071866/non-blocking-subprocess-call) (although answers there doesn't explain how to wait for processes to finish) – user202729 Feb 16 '21 at 15:51

1 Answers1

2

The target needs to be a callable; it's invoked with some given arguments.

For your use case, os.system will do as a target:

import threading
import os

if __name__ == "__main__":
    exes = [
        "C:\\Users\\XYZ\\PycharmProjects\\Task1\\dist\\Multiof2.exe",
        "C:\\Users\\XYZ\\PycharmProjects\\Task2\\dist\\Multiof5.exe",
    ]
    threads = [
        threading.Thread(target=os.system, args=(exe,)) for exe in exes
    ]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()

However, there's no real need to use a thread to wait for the subprocess; you can just use the subprocess module:

import subprocess
if __name__ == "__main__":
    exes = [
        "C:\\Users\\XYZ\\PycharmProjects\\Task1\\dist\\Multiof2.exe",
        "C:\\Users\\XYZ\\PycharmProjects\\Task2\\dist\\Multiof5.exe",
    ]
    procs = [subprocess.Popen(exe) for exe in exes]
    for proc in procs:
        proc.wait()
        if proc.returncode != 0:
            print(proc, "failed")
AKX
  • 152,115
  • 15
  • 115
  • 172
  • above code works but `proc.wait(timeout=15)` is not working. I also raised TimeoutExpired Exception but also not working. The above process is executing even after a timeout. [Above 2 .exe's will take 10 and 30 secs to complete execution]. I also tried Popen(exe, timeout=15) - got 'TypeError'. –  Feb 16 '21 at 18:09
  • You would need to kill the processes if you don't want them to continue executing... `proc.terminate()`. – AKX Feb 16 '21 at 18:45
  • after timeout only I have to kill the process. but timeout not working. –  Feb 16 '21 at 18:52
  • `.wait()` waits for the process to stop or the timeout to occur. You'll need to check whether it stopped already and if not, kill it. Show your code and your intent in a new question and link it here and we can help. – AKX Feb 16 '21 at 19:03
  • https://stackoverflow.com/questions/66230928/subprocess-waittimeout-15-is-not-working –  Feb 16 '21 at 19:28