Can somebody help me to understand the following code snippet ? I know that I must not use global variables using multiprocessing
but I'm still suprised about the result I see.
I use a global file handle within the function executed remotely in a different worker process.
import multiprocessing
import os
fh = open("out.txt", "w")
def process(i):
print("i={} pid={} id(fh)={}".format(i, os.getpid(), id(fh)))
print(i, file=fh)
def main():
p = multiprocessing.Pool(3)
p.map(process, (1, 2, 3))
p.terminate()
fh.close()
main()
The output is
i=1 pid=92045 id(fh)=4314964256
i=2 pid=92046 id(fh)=4314964256
i=3 pid=92047 id(fh)=4314964256
So we see that there are three different process ids as expected.
What suprises me:
- the unpickable file handle is available in the worker processes
- the memory address computed by
id
is the same for all workers - the worker process can write to this file handle without throwing an exception
- nevertheless the file is empty after programm execution.