@@ -120,6 +120,22 @@ def wrapper(*args, **kwargs):
120120 return wrapper
121121
122122
123+ def async_gen_wrapper (func , obj = None ):
124+ """Given a async generator, make so can be called in blocking contexts"""
125+
126+ @functools .wraps (func )
127+ def wrapper (* args , ** kwargs ):
128+ self = obj or args [0 ]
129+ gen = func (* args , ** kwargs )
130+ while True :
131+ try :
132+ yield sync (self .loop , gen .__anext__ )
133+ except StopAsyncIteration :
134+ break
135+
136+ return wrapper
137+
138+
123139def get_loop ():
124140 """Create or return the default fsspec IO loop
125141
@@ -959,7 +975,7 @@ def mirror_sync_methods(obj):
959975 """
960976 from fsspec import AbstractFileSystem
961977
962- for method in async_methods + dir (AsyncFileSystem ):
978+ for method in set ( async_methods + dir (AsyncFileSystem ) ):
963979 if not method .startswith ("_" ):
964980 continue
965981 smethod = method [1 :]
@@ -969,11 +985,15 @@ def mirror_sync_methods(obj):
969985 is_default = unsync is getattr (AbstractFileSystem , smethod , "" )
970986 if isco and is_default :
971987 mth = sync_wrapper (getattr (obj , method ), obj = obj )
972- setattr (obj , smethod , mth )
973- if not mth .__doc__ :
974- mth .__doc__ = getattr (
975- getattr (AbstractFileSystem , smethod , None ), "__doc__" , ""
976- )
988+ elif inspect .isasyncgenfunction (getattr (obj , method , None )) and is_default :
989+ mth = async_gen_wrapper (getattr (obj , method ), obj = obj )
990+ else :
991+ continue
992+ setattr (obj , smethod , mth )
993+ if not mth .__doc__ :
994+ mth .__doc__ = getattr (
995+ getattr (AbstractFileSystem , smethod , None ), "__doc__" , ""
996+ )
977997
978998
979999class FSSpecCoroutineCancel (Exception ):
0 commit comments