我希望能够一次选择多个文件并使用subprocess命令运行它们,并认为使用线程是最好的。 这是我的代码:
i=0
IMLCPath = raw_input("Enter the path for IMLC: ")
Path_to_execute = raw_input("Enter the path where IMLC has to be executed: ")
NumberofThreads =raw_input("Enter the number of threads to be run in parallel: ")
def foo(filename):
subprocess.call(['imlc.exe', filename],shell=True)
def Capture():
global i
for filename in glob.glob(os.path.join(Path_to_execute, '*.xml')):
os.chdir(IMLCPath)
D = threading.Thread(target = foo, args = (filename,))
D.start()
if i%float(NumberofThreads) == 0:
D.join()
if __name__ == '__main__':
Capture()
但是当我运行它时,同一文件名被运行两次(如果NumberofThreads = 2)。我该如何解决?应该从该文件夹中取出2个文件,然后同时运行它们