-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into add-implementations
- Loading branch information
Showing
21 changed files
with
551 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import asyncio | ||
import inspect | ||
import functools | ||
from fsspec.asyn import AsyncFileSystem | ||
|
||
|
||
def async_wrapper(func, obj=None): | ||
""" | ||
Wraps a synchronous function to make it awaitable. | ||
Parameters | ||
---------- | ||
func : callable | ||
The synchronous function to wrap. | ||
obj : object, optional | ||
The instance to bind the function to, if applicable. | ||
Returns | ||
------- | ||
coroutine | ||
An awaitable version of the function. | ||
""" | ||
|
||
@functools.wraps(func) | ||
async def wrapper(*args, **kwargs): | ||
return await asyncio.to_thread(func, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
class AsyncFileSystemWrapper(AsyncFileSystem): | ||
""" | ||
A wrapper class to convert a synchronous filesystem into an asynchronous one. | ||
This class takes an existing synchronous filesystem implementation and wraps all | ||
its methods to provide an asynchronous interface. | ||
Parameters | ||
---------- | ||
sync_fs : AbstractFileSystem | ||
The synchronous filesystem instance to wrap. | ||
""" | ||
|
||
def __init__(self, sync_fs, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.asynchronous = True | ||
self.fs = sync_fs | ||
self._wrap_all_sync_methods() | ||
|
||
@property | ||
def fsid(self): | ||
return f"async_{self.fs.fsid}" | ||
|
||
def _wrap_all_sync_methods(self): | ||
""" | ||
Wrap all synchronous methods of the underlying filesystem with asynchronous versions. | ||
""" | ||
for method_name in dir(self.fs): | ||
if method_name.startswith("_"): | ||
continue | ||
|
||
attr = inspect.getattr_static(self.fs, method_name) | ||
if isinstance(attr, property): | ||
continue | ||
|
||
method = getattr(self.fs, method_name) | ||
if callable(method) and not asyncio.iscoroutinefunction(method): | ||
async_method = async_wrapper(method, obj=self) | ||
setattr(self, f"_{method_name}", async_method) | ||
|
||
@classmethod | ||
def wrap_class(cls, sync_fs_class): | ||
""" | ||
Create a new class that can be used to instantiate an AsyncFileSystemWrapper | ||
with lazy instantiation of the underlying synchronous filesystem. | ||
Parameters | ||
---------- | ||
sync_fs_class : type | ||
The class of the synchronous filesystem to wrap. | ||
Returns | ||
------- | ||
type | ||
A new class that wraps the provided synchronous filesystem class. | ||
""" | ||
|
||
class GeneratedAsyncFileSystemWrapper(cls): | ||
def __init__(self, *args, **kwargs): | ||
sync_fs = sync_fs_class(*args, **kwargs) | ||
super().__init__(sync_fs) | ||
|
||
GeneratedAsyncFileSystemWrapper.__name__ = ( | ||
f"Async{sync_fs_class.__name__}Wrapper" | ||
) | ||
return GeneratedAsyncFileSystemWrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.