简体   繁体   中英

gcsfs async interface does not seem to be working (error using simple example)

I am on Linux/Python3.8.5

python3 -m pip list | grep gcsfs
gcsfs                    2021.4.0

I looked at the docs, specifically chapter 5 - Async: https://buildmedia.readthedocs.org/media/pdf/gcsfs/stable/gcsfs.pdf

I also found an example from here: https://github.com/dask/gcsfs/issues/285 , which is shown below:

import asyncio
import gcsfs

async def main():
    loop = asyncio.get_event_loop()
    fs = gcsfs.GCSFileSystem(project="my_project", asynchronous=True, loop=loop)
    await fs.set_session()

    async with await fs.open("my_bucket/my_blob") as fp:
        b = await fp.read()
    print(len(b))

asyncio.get_event_loop().run_until_complete(main())

The error is:

Traceback (most recent call last):
  File "test_gcsfs_async.py", line 13, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "test_gcsfs_async.py", line 7, in main
    await fs.set_session()
AttributeError: 'GCSFileSystem' object has no attribute 'set_session'

If I simply remove the call to set_session(), then I get this error:

Traceback (most recent call last):
  File "test_gcsfs_async.py", line 13, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "test_gcsfs_async.py", line 9, in main
    async with await fs.open("my_bucket/my_blob") as fp:
  File "/usr/local/lib/python3.8/dist-packages/fsspec/spec.py", line 942, in open
    f = self._open(
  File "/usr/local/lib/python3.8/dist-packages/gcsfs/core.py", line 1247, in _open
    return GCSFile(
  File "/usr/local/lib/python3.8/dist-packages/gcsfs/core.py", line 1378, in __init__
    super().__init__(
  File "/usr/local/lib/python3.8/dist-packages/fsspec/spec.py", line 1270, in __init__
    self.details = fs.info(path)
  File "/usr/local/lib/python3.8/dist-packages/fsspec/asyn.py", line 72, in wrapper
    return sync(self.loop, func, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/fsspec/asyn.py", line 40, in sync
    raise RuntimeError("Loop is not running")
RuntimeError: Loop is not running

Unfortunately, the file-like interface of GCSFile is not async-compatible. From the issue you linked:

The file-like interface itself is not asynchronous, since there is state (the current buffers and file positions).

This may be implemented in the future, but would require a certain amount of work.

Note that the coroutine method you were after is called _set_session (with the leading underscore). This should be better documented - feel free to raise an issue or submit a PR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM