0

I am trying to pass file object to different processes using Manager().dict, but I found the Manager().dict will close file automatically. I wonder what is happened and how to keep file open.

import multiprocessing as mp

fileHandler =  open('test.csv', 'r')
d = mp.Manager().dict()
d['a'] = fileHandler
print d
print d['a'].closed 

output:

{'a': <closed file '<uninitialized file>', mode '<uninitialized file>' at 0x7f474f45f540>}
True
martineau
  • 119,623
  • 25
  • 170
  • 301
galaxyan
  • 5,944
  • 2
  • 19
  • 43
  • You can't pass open file objects between processes. Perhaps the workaround would be to pass just the file name. – martineau Jun 05 '19 at 16:18
  • @martineau good to know. I wonder if you why the Manager().dict close file automatically – galaxyan Jun 05 '19 at 19:49
  • 1
    No, not offhand. Generally speaking, `multiprocessing` uses `pickle` to share objects an it doesn't support open files. If you really want to share one among multiple processes, you could create your own server process manager to hold it which other process could use to access it. See my answer to the question [Working with queue object across multiple processes](https://stackoverflow.com/questions/54511731/working-with-queue-object-across-multiple-processes). Also see [Mangers](https://docs.python.org/3/library/multiprocessing.html#managers) in the multiprocessing documentation. – martineau Jun 05 '19 at 21:02

0 Answers0