Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import aiomysql
- from threading import Thread
- def start_loop(loop):
- asyncio.set_event_loop(loop)
- loop.run_forever()
- async def create_pool(loop, **kw):
- global __pool
- __pool = await aiomysql.create_pool(
- host=kw.get('host', 'localhost'),
- port=kw.get('port', 3306),
- user=kw['user'],
- password=kw['password'],
- db=kw['db'],
- charset=kw.get('charset', 'utf8'),
- autocommit=kw.get('autocommit', True),
- maxsize=kw.get('maxsize', 10),
- minsize=kw.get('minsize', 1),
- loop=loop
- )
- async def select():
- global __pool
- async with __pool.acquire() as conn:
- async with conn.cursor(aiomysql.DictCursor) as cur:
- print('ok here')
- await cur.execute('select * from test')
- print('failed here')
- rs = await cur.fetchall()
- print('rows returned: %s' % len(rs))
- loop = asyncio.get_event_loop()
- new_loop = asyncio.new_event_loop()
- t = Thread(target=start_loop, args=(new_loop,))
- loop.run_until_complete(create_pool(loop, user='user', password='password', db='test'))
- t.start()
- asyncio.run_coroutine_threadsafe(select(), new_loop)
Add Comment
Please, Sign In to add comment