The process cannot access the file because it is being used by another process in python

1.3k Views Asked by At
 def worker(ip, started_date, full_path):


    planner_file = full_path+"\\"+"export_"+str(ip)+".txt"

    print "planner file", planner_file

    arg_list = []
    action ="MakeExport"
    arg_list.append(upnp_path)
    arg_lista.append(action)
    arg_list.append(' ip=')
    arg_list.append(ip)
    arg_list.append(" 2>NULL")


    command = ['python', arg_list]

    p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)

    output = p.stdout.read()

   with open(planner_file, "a") as pf:
       pf.write(output)




if __name__=='__main__':

    rack_number, started_date, full_path, ips = main()

    pid = os.getpid()

   print('Main Process is started and PID is: ' + str(pid))

   process_list = []

   for ip in ips:
        p = Process(target=worker, args=(ip, started_date, full_path))
        p.start()
        child_pid = str(p.pid)
        print('PID is:' + child_pid)
        process_list.append(child_pid)

   children = multiprocessing.active_children()

   # print process_list

  while children != []:
     time.sleep(1)
     children = multiprocessing.active_children()

I am trying to export some ip addresses data to txt files. While it works OK with single ip address, if I try my script for multiple ip addresses, I get the following error in the all other ip txt file.

For the first ip address, script exports data correctly. For all other remaining ips it says;

'The process cannot access the file because it is being used by another process.'

For example : export_1.8.5.20.txt (First ip data exported correctly) export_1.8.5.21.txt (it says the process can not access the file)

I do not get any errors from my script. Just get the above details inside the txt files.

Any helps would be appreciated. Thanks

1

There are 1 best solutions below

0
On

This seems to be the known bug: https://bugs.python.org/issue33369 or https://bugs.python.org/issue19575

Try to put a lock around the Popen() call.