K
K
kr_ilya2020-05-13 12:49:40
Python
kr_ilya, 2020-05-13 12:49:40

How to fix a bug with asyncpg pool?

Trying to add records to the database in a loop, the
5ebbc2413a077711276322.png

t.py error appears

import asyncio
import base


async def start():
  q = 0
  while q < 5:
    q += 1
    await base.insUrl('link')


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(start())
    loop.close()


base.py
import asyncpg

pool = asyncpg.create_pool(user='postgres', password= '123456', host='localhost', database='rydu', command_timeout=1500000, max_queries=1500000)
con = pool.acquire()

async def insUrl(url):
  try:
    await con.execute('INSERT INTO links (link, status) VALUES($1, $2) ', url, 200)
  finally:
    await pool.release(con)

async def close():
  await con.close()


How is the problem, how to fix it?

async pool

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-05-13
@kr_ilya

1. deal with the basics of asyncio, understand what is and how the event loop, async and await work
2. carefully read the docs, even the examples indicate the correct call to create_pool and acquire
PS without good knowledge of python, you can not climb into asyncio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question