Skip to content

Using async iterator in copy_records_to_table #689

Closed

Description

@alex-eri
  • asyncpg version: 0.22.0
  • PostgreSQL version: 13
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : no
  • Python version: 3.8, 3.9
  • Platform: Windows, Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: git
  • If you built asyncpg locally, which version of Cython did you use?: 0.29.21
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : yes

I want to stream data to base with COPY, but i have async code, that produce data. I can pass regular iterator to records, but it ends then Queue empty. So reconnect needed.

queue = asyncio.Queue(2<<10)
loop.create_task(feed(queue))

async def get_records(queue):
    while True:
        r = await queue.get()
        yield r
        self.q.task_done()

async with pg.acquire() as connection:
    await connection.copy_records_to_table(
                     'radarlog2',
                     records = get_records(queue)
                 )

Activity

  1. changed the title [-]Using async iterator in [/-] [+]Using async iterator in copy_records_to_table[/+] on Jan 14, 2021
  2. robhaswell commented on Mar 8, 2021

    @robhaswell

    I too was surprised that this wasn't supported.

    It seems that copy_to_table supports an async iterator but it must generate the Postgres binary protocol? I couldn't find a protocol formatter anywhere even though I assume that asyncpg must contain one somewhere in order to support this function.

    If anyone has some guidance on how to use copy_to_table then that would be a good alternative, thanks.

  3. added a commit that references this issue on Mar 8, 2021
    9f6839b
  4. elprans commented on Mar 8, 2021

    @elprans
    Member

    #713 adds support for async iterables in copy_records_to_table.

  5. added a commit that references this issue on Mar 8, 2021
    7216850
  6. robhaswell commented on Mar 9, 2021

    @robhaswell

    Thanks!

  7. added 2 commits that reference this issue on Mar 24, 2021
    f26cd38
    ef27ad5
  8. added 3 commits that reference this issue on Aug 2, 2021
    e65c892
    e728d54
    c416100
  9. added a commit that references this issue on Aug 10, 2021
    1d33ff6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions