1

I am writing a python script which will iterate a directory and run a command based on the file,like this:

for root, directories,files in os.walk(directory):
    for filename in files:
        if filename.endswith('.xx'):
            filepath=os.path.join(root,filename)
                            process_file(filepath)


def process_file(filepath):
    #get the file name and extension
    name,ext=os.path.splitext(os.path.basename(filepath))

    #copy the file to a tmp file, because the filepath may contain some no-asci character
    tmp_file_src='c:/'+uuid.uuid1()+'.xx'
    tmp_file_dest=tmp_file_src.replace('.xx','.yy')
    shutil.copy2(filepath,tmp_file_src)

    #run the command which may take 10-20 seconds
    os.system('xx %s %s' %(tmp_file_src,tmp_file_dest))

    #copy the generated output file, reset the file name
    shutil.copy2(tmp_file_dest,os.path.dirname(filepath)+'/'+name+'.yy')

As you can see, one file to one command, and I have to wait the command run completely to do the further job.

Not the execute process:

file1-->file2.....

I wonder if they can be executed parallelly?

file1
file2
....
hguser
  • 35,079
  • 54
  • 159
  • 293
  • 3
    You should be able to run them in parallel with `subprocess.Popen` instead of `os.system` since the former doesn't block. – mgilson Jan 08 '14 at 08:57
  • possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – ereOn Jan 08 '14 at 08:58
  • @mgilson:Then how do I know when one of the execution complete? Since I have handle the result of each execution. – hguser Jan 08 '14 at 09:00
  • This [link](http://www.pythonforbeginners.com/systems-programming/subprocess-for-system-administrators/) can help you... – Kobi K Jan 08 '14 at 09:00
  • I am sorry but I do not think this is duplicated, since the post "Calling an external command in Python" just need to run command , the author does not say that he care about the result. But I need to run command parallelly and also I have to get the result of each execution. – hguser Jan 08 '14 at 09:05

1 Answers1

0

There is even the possibility to use threading module.

import threading

def starter_function(cmd_to_execute):
    os.system(cmd_to_execute)

execution_thread = threading.Thread(target=starter_function, args=(cmd_to_execute,))

execution_thread.start()
imizeropt
  • 176
  • 1
  • 13