Skip to content

python: Future object cannot be converted to PyBytes #2568

@gaby

Description

@gaby

I'm trying to write a PoC for Async Reading/Writing files using opendal python bindings and ran into an error when trying to use the reader as an async context.

import asyncio
import opendal

async def main():
	op = opendal.AsyncOperator("fs", root="./git/test/opendal")

	async with op.open_reader("1g.img") as reader:
		await op.write("new.img", await reader.read(size=1024*1024))

asyncio.run(main())

Output:

$ python3 async-writter.py 
Traceback (most recent call last):
  File "/home/ubuntu/Desktop/git/test/opendal/async-writter.py", line 11, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/ubuntu/Desktop/git/test/opendal/async-writter.py", line 9, in main
    await op.write("new.img", reader.read(size=1024*1024))
TypeError: argument 'bs': 'Future' object cannot be converted to 'PyBytes'

How is this suppose to work? The docs state reader.read() returns bytes, and the Write() function also takes bytes. Somehow there's a conflict of types here for a simple example.

Also, it seems when running async with op.open_reader("1g.img") alone it reads the whole file into memory before doing anything.

Extra information:

$ python3 --version
Python 3.10.6
$ pip3 list | grep -i opendal
opendal                 0.38.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions