I'm having a strange issue where Python will successfully find and read a binary file that exists, but pickle.load()
will not. pickle.load()
is throwing a FileNotFoundError which doesn't make much sense. I know for a fact the file is there because if I try to read the contents of the file I'm able to.
try:
with open("test", "rb") as f:
print(f.read())
data = pickle.load(f)
except FileNotFoundError as e:
print(e)
I've been trying to wrap my head around this for a few hours now and I just can't understand what's going on here. I've had my fair share of Python and never had this happen to me. Working on Windows 10 with VSCode and WSL (Ubuntu 20.04).
EDIT: I know this particular code won't work because I'm reading with f.read()
first. I just put it there to show that it works, I only really want to pickle.load()
it.
EDIT: Traceback goes like such:
Traceback (most recent call last):
File "/mnt/d/_/Projects/FCUL/SO/pgrepwc/v2/hpgrepwc.py", line 34, in main
data = pickle.load(f)
File "/usr/lib/python3.8/multiprocessing/managers.py", line 959, in RebuildProxy
return func(token, serializer, incref=incref, **kwds)
File "/usr/lib/python3.8/multiprocessing/managers.py", line 809, in __init__
self._incref()
File "/usr/lib/python3.8/multiprocessing/managers.py", line 863, in _incref
conn = self._Client(self._token.address, authkey=self._authkey)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 502, in Client
c = SocketClient(address)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 630, in SocketClient
s.connect(address)
FileNotFoundError: [Errno 2] No such file or directory
And as requested, my directory listing:
prgrepwc:
|
| histFile1
| histFile2
| .gitattributes
| .gitignore
| testFile
|
+---.vscode
| launch.json
| settings.json
|
|
\---v2
|
| testFile
| histFile
| hpgrepwc.py
| Load.py
| Match.py
| pgrepwc_v2.py
\-- README.txt
The file I'm executing is hpgrepwc.py
in folder v2
. The file I'm trying to read is the binary file testFile
. I've noticed even though my script is in folder v2
, it defaults to pgrepwc
sometimes so I even placed a copy of testFile
on there just in case. No dice either way, I've also tried to save the file as .bin
to no avail.
SOLUTION:
@tdelaney mentioned:
"(...) it looks like some object created in a multiprocessing.Manager was pickled. But these objects are actually proxies that broadcast changes to a group of subprocesses and are not valid outside of that context. In your case, the unpickler tried to reconstruct a class that tried to reconnect to its long-dead multiprocessing partners. You need to look to the code doing the pickling and figure out some other way to encapsulate the data."
This was exactly it. I make heavy use of multiprocessing.Manager
for shared memory data structures in my code. After converting a manager.dict()
to a regular Python dict
, the pickling and unpickling worked like a charm. Once again, thanks to everyone who contributed and especially @tdelaney.