Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add __reduce__ for AbstractBufferedFile #1751

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions fsspec/implementations/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,25 +692,6 @@ async def async_fetch_range(self, start, end):

_fetch_range = sync_wrapper(async_fetch_range)

def __reduce__(self):
return (
reopen,
(
self.fs,
self.url,
self.mode,
self.blocksize,
self.cache.name if self.cache else "none",
self.size,
),
)


def reopen(fs, url, mode, blocksize, cache_type, size=None):
return fs.open(
url, mode=mode, block_size=blocksize, cache_type=cache_type, size=size
)


magic_check = re.compile("([*[])")

Expand Down Expand Up @@ -760,9 +741,6 @@ def close(self):
asyncio.run_coroutine_threadsafe(self._close(), self.loop)
super().close()

def __reduce__(self):
return reopen, (self.fs, self.url, self.mode, self.blocksize, self.cache.name)


class AsyncStreamFile(AbstractAsyncStreamedFile):
def __init__(
Expand Down
23 changes: 23 additions & 0 deletions fsspec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,22 @@ def seekable(self):
def writable(self):
"""Whether opened for writing"""
return self.mode in {"wb", "ab"} and not self.closed

def __reduce__(self):
if self.mode != "rb":
raise RuntimeError("Pickling a writeable file is not supported")

return reopen, (
self.fs,
self.path,
self.mode,
self.blocksize,
self.loc,
self.size,
self.autocommit,
self.cache.name if self.cache else "none",
self.kwargs,
)

def __del__(self):
if not self.closed:
Expand All @@ -2064,3 +2080,10 @@ def __enter__(self):

def __exit__(self, *args):
self.close()


def reopen(fs, path, mode, blocksize, loc, size, autocommit, cache_type, kwargs):
file = fs.open(path, mode=mode, block_size=blocksize, autocommit=autocommit, cache_type=cache_type, size=size, **kwargs)
if loc > 0:
file.seek(loc)
return file