进程无法访问该文件,因为它正被python中的另一个进程使用

2024-04-25 05:55:40 发布

您现在位置:Python中文网/ 问答频道 /正文

 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()

我正在尝试将一些ip地址数据导出到txt文件。虽然它可以在单个ip地址下工作,但是如果我尝试将脚本用于多个ip地址,那么在所有其他ip txt文件中会出现以下错误。在

对于第一个ip地址,脚本正确导出数据。对于所有其他剩余的IP地址

'该进程无法访问该文件,因为它正被另一个进程使用。

例如: export_1.8.5.20.txt(第一个正确导出的ip数据) export_1.8.5.21.txt(表示进程无法访问文件)

我的脚本没有任何错误。只需在txt文件中获取上述详细信息。在

任何帮助都将不胜感激。谢谢


Tags: 文件pathiptxt地址argpidfull