![](/img/trans.png)
[英]Python - The process cannot access the file because it is being used by another process:
[英]The process cannot access the file because it is being used by another process in 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 文件中获取上述详细信息即可。
任何帮助将不胜感激。 谢谢
这似乎是已知的错误: https : //bugs.python.org/issue33369或https://bugs.python.org/issue19575
尝试在 Popen() 调用周围加锁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.