Reputation:
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.
Upvotes: 1
Views: 1229
Reputation: 169388
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")
Upvotes: 2