0

I trying to learn how to use the asyncio framework in Python. I have the below code but it is giving the error:

event loop stopped before the future completed.

Also please update where to capture the outputs of the code.

Code:

import asyncio
import aiopg

dsn = 'dbname=dvdrental user=postgres password=password host=127.0.0.1'


async def go():
    pool = await aiopg.create_pool(dsn)
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("Select * from actor")
            ret = []
            async for row in cur:
                ret.append(row)
            assert ret == [(1,)]

loop = asyncio.get_event_loop()
loop.run_until_complete(go())
1
  • What did you find when you searched for that error? Commented Feb 8, 2018 at 2:56

1 Answer 1

2

The pool is not closed before exit from go() coroutine and loop stopping.

Use async with aiopg.create_pool() instead of await aiopg.create_pool() or explicitly call pool.close()/await pool.await_closed() at the end of coroutine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.