aiomysql icon indicating copy to clipboard operation
aiomysql copied to clipboard

Support Python 3.7's asyncio.run() syntax~

Open leafonsword opened this issue 5 years ago • 7 comments

As of Python 3.7, asyncio has many new easy-to use feature, aiomysql's usage syntax is long and difficult to use;

Hope asyncio could support Python 3.7's new feature such as asyncio.run(), asyncio.create_task(), asyncio.gather()

leafonsword avatar May 24 '19 09:05 leafonsword

Please elaborate your proposal

asvetlov avatar May 24 '19 09:05 asvetlov

@Askaholic with Python 3.7's new feature--asyncio.run(), asyncio.create_task(), asyncio.gather(), async syntax is simple and elegant, for example:

import pendulum 
import asyncio

async def test(name):
    print(f'Begin:{name} {pendulum.now()}')
    await asyncio.sleep(2)
    print(f'End:{name} {pendulum.now()}')
    
async def main():
    task1 = asyncio.create_task(test('A'))
    task2 = asyncio.create_task(test('B'))
    
    await task1
    await task2

asyncio.run(main())
import pendulum 
import asyncio

async def test(name):
    print(f'Begin:{name} {pendulum.now()}')
    await asyncio.sleep(2)
    print(f'End:{name} {pendulum.now()}')
    
async def main():
   await asyncio.gather(
        test('Bob'),
        test('Amy'),
        test('Mike'),
    )
    
asyncio.run(main())

leafonsword avatar May 28 '19 02:05 leafonsword

What prevents you to use this elegant syntax with aiomysql right now?

asvetlov avatar May 28 '19 07:05 asvetlov

@leafonsword aiomysql's document doesn't have asyncio.run(), asyncio.create_task(), asyncio.gather() syntax example, just this:

import asyncio
import aiomysql

loop = asyncio.get_event_loop()

@asyncio.coroutine
def test_example():
    conn = yield from aiomysql.connect(host='127.0.0.1', port=3306,
                                       user='root', password='', db='mysql',
                                       loop=loop)

    cur = yield from conn.cursor()
    yield from cur.execute("SELECT Host,User FROM user")
    print(cur.description)
    r = yield from cur.fetchall()
    print(r)
    yield from cur.close()
    conn.close()

loop.run_until_complete(test_example())

leafonsword avatar May 28 '19 07:05 leafonsword

But all these things just work :) Would you communicate with @terrycain and propose pull request for docs improvement. run_until_complete should stay for a while making python 3.7 the minimal supported version. Otherwise, it confuses people who use Python 3.6 for example. yield from can be replaced with await right now.

asvetlov avatar May 28 '19 08:05 asvetlov

@asvetlov I'm still learning asyncio, after I dive into async syntax, I will try to using asyncio.run(), asyncio.create_task(), asyncio.gather() with aiomysql, and push a pr

leafonsword avatar May 28 '19 09:05 leafonsword

Be warned that if you use asyncio.gather() then you need a new connection per coroutine, otherwise you'll end up with invalid packet orders. Othewise you end up with all the running coroutines sharing the same connection which means when a different coroutine is scheduled to run, the connection could be in the middle of a different query and therefore you'll get packet out of order errors.

huwcbjones avatar May 28 '20 10:05 huwcbjones