BytesIO()对象是完全加载到内存中还是流式加载?

2024-09-27 20:19:36 发布

您现在位置:Python中文网/ 问答频道 /正文

上下文

我有一个比较大的表(大约30gbs),我想从Postgres转移到S3。我正试图了解像io.BytesIO()这样的文件是如何工作的,我需要在机器上提供多少内存才能更好地设计代码

我所尝试的

我使用aiobotcore将数据移动到s3,使用asyncpg查询Postgres。为了更好地解释这个问题,我制作了一个示例演示

import asyncio
import asyncpg
import io
import aiobotocore 


async def happy_demo():
    # Define the connection to Postgres
    con = await asyncpg.connect(**postgres_credentials)
    # Get a handle onto an aiobotocore session
    session = aiobotocore.get_session()
    # Create file-like object
    file = io.BytesIO()
    # Create the S3 client
    async with session.create_client(**aws_credentials) as client:
        # Create a gzip file
        with gzip.GzipFile(fileobj=file, mode='wb') as gz:
            await con.copy_from_query(query='select * from bar', output=gz, format='csv')
            # Write to S3
            await client.put_object(Bucket="happy_bucket", Key="foo.csv", Body=file.getvalue())
            # Close the connection.
            await con.close()


def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(happy_demo())


if __name__ == '__main__':
    main()

我想了解的是

特别是,我试图理解,如果表大小在30gb左右,我是否需要一台至少有30gb ram的机器才能执行此操作

更新

我已经更新了我的代码(包括gzip文件),并稍微重新格式化了我的问题以包括gzip


Tags: theioimportclients3sessioncreatepostgres

热门问题