Skip to content

Commit 671ff26

Browse files
authored
Make asynchronous optional in async_wrapper (#1794)
1 parent a14a1dd commit 671ff26

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

fsspec/implementations/asyn_wrapper.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import functools
33
import inspect
44

5-
from fsspec.asyn import AsyncFileSystem
5+
from fsspec.asyn import AsyncFileSystem, running_async
66

77

88
def async_wrapper(func, obj=None):
@@ -42,10 +42,14 @@ class AsyncFileSystemWrapper(AsyncFileSystem):
4242
The synchronous filesystem instance to wrap.
4343
"""
4444

45-
def __init__(self, sync_fs, *args, **kwargs):
46-
super().__init__(*args, **kwargs)
47-
self.asynchronous = True
48-
self.sync_fs = sync_fs
45+
protocol = "async_wrapper"
46+
cachable = False
47+
48+
def __init__(self, fs, *args, asynchronous=None, **kwargs):
49+
if asynchronous is None:
50+
asynchronous = running_async()
51+
super().__init__(*args, asynchronous=asynchronous, **kwargs)
52+
self.sync_fs = fs
4953
self.protocol = self.sync_fs.protocol
5054
self._wrap_all_sync_methods()
5155

fsspec/implementations/dirfs.py

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ def __init__(
3636
super().__init__(**storage_options)
3737
if fs is None:
3838
fs = filesystem(protocol=target_protocol, **(target_options or {}))
39-
if (path is not None) ^ (fo is not None) is False:
40-
raise ValueError("Provide path or fo, not both")
4139
path = path or fo
4240

4341
if self.asynchronous and not fs.async_impl:

fsspec/implementations/tests/test_asyn_wrapper.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
from .test_local import csv_files, filetexts
1111

1212

13-
def test_is_async():
13+
@pytest.mark.asyncio
14+
async def test_is_async_default():
1415
fs = fsspec.filesystem("file")
1516
async_fs = AsyncFileSystemWrapper(fs)
1617
assert async_fs.async_impl
18+
assert async_fs.asynchronous
19+
async_fs = AsyncFileSystemWrapper(fs, asynchronous=False)
20+
assert not async_fs.asynchronous
1721

1822

1923
def test_class_wrapper():
@@ -53,6 +57,7 @@ async def test_cats():
5357
assert result == b"a,b\n1,2\n"[1:-2]
5458

5559
# test synchronous API is available as expected
60+
async_fs = AsyncFileSystemWrapper(fs, asynchronous=False)
5661
result = async_fs.cat(".test.fakedata.1.csv", start=1, end=-2)
5762
assert result == b"a,b\n1,2\n"[1:-2]
5863

@@ -142,3 +147,17 @@ async def test_batch_operations():
142147
await async_fs._rm([".test.fakedata.1.csv", ".test.fakedata.2.csv"])
143148
assert not await async_fs._exists(".test.fakedata.1.csv")
144149
assert not await async_fs._exists(".test.fakedata.2.csv")
150+
151+
152+
def test_open(tmpdir):
153+
fn = f"{tmpdir}/afile"
154+
with open(fn, "wb") as f:
155+
f.write(b"hello")
156+
of = fsspec.open(
157+
"dir://afile::async_wrapper::file",
158+
mode="rb",
159+
async_wrapper={"asynchronous": False},
160+
dir={"path": str(tmpdir)},
161+
)
162+
with of as f:
163+
assert f.read() == b"hello"

fsspec/implementations/tests/test_reference.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,10 @@ def test_fss_has_defaults(m):
515515
assert fs.fss[None] is m
516516

517517
fs = fsspec.filesystem("reference", fo={"key": ["memory://a"]})
518-
assert fs.fss[None] is fs.fss["memory"]
518+
assert fs.fss[None] == fs.fss["memory"]
519519

520520
fs = fsspec.filesystem("reference", fo={"key": ["memory://a"], "blah": ["path"]})
521-
assert fs.fss[None] is fs.fss["memory"]
521+
assert fs.fss[None] == fs.fss["memory"]
522522

523523

524524
def test_merging(m):

fsspec/registry.py

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def register_implementation(name, cls, clobber=False, errtxt=None):
7272
"class": "fsspec.implementations.arrow.HadoopFileSystem",
7373
"err": "pyarrow and local java libraries required for HDFS",
7474
},
75+
"async_wrapper": {
76+
"class": "morefs.asyn_wrapper.AsyncWrapperFileSystem",
77+
},
7578
"asynclocal": {
7679
"class": "morefs.asyn_local.AsyncLocalFileSystem",
7780
"err": "Install 'morefs[asynclocalfs]' to use AsyncLocalFileSystem",

0 commit comments

Comments
 (0)