Windowsでマルチプロセッシングを使用すると、開いているファイルハンドルはすべて生成されたプロセスに継承されているように見えます。これには、それらをロックするという不快な副作用があります。
私は次のいずれかに興味があります:
1)継承の防止
2)生成されたプロセスからファイルを解放する方法
OSXでは正常に動作するが、os.renameのWindowsではクラッシュする次のコードについて考えてみます。
from multiprocessing import Process
import os
kFileA = "a.txt"
kFileB = "b.txt"
def emptyProcess():
while 1:
pass
def main():
# Open a file and write a message
testFile = open(kFileA, 'a')
testFile.write("Message One\n")
# Spawn a process
p = Process(target=emptyProcess)
p.start()
# Close the file
testFile.close()
# This will crash
# WindowsError: [Error 32] The process cannot access the file
# because it is being used by another process
os.rename(kFileA, kFileB)
testFile = open(kFileA, 'a')
testFile.write("Message Two\n")
testFile.close()
p.terminate()
if __name__ == "__main__":
main()