PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/README.rst

https://gitlab.com/carmes/aiomysql
ReStructuredText | 146 lines | 101 code | 45 blank | 0 comment | 0 complexity | af984a1da647ab2622e242e5195080ec MD5 | raw file
  1. aiomysql
  2. ========
  3. .. image:: https://travis-ci.org/aio-libs/aiomysql.svg?branch=master
  4. :target: https://travis-ci.org/aio-libs/aiomysql
  5. .. image:: https://coveralls.io/repos/aio-libs/aiomysql/badge.svg
  6. :target: https://coveralls.io/r/aio-libs/aiomysql
  7. .. image:: https://badge.fury.io/py/aiomysql.svg
  8. :target: https://badge.fury.io/py/aiomysql
  9. :alt: Latest Version
  10. .. image:: https://readthedocs.org/projects/aiomysql/badge/?version=latest
  11. :target: https://aiomysql.readthedocs.io/
  12. :alt: Documentation Status
  13. **aiomysql** is a "driver" for accessing a `MySQL` database
  14. from the asyncio_ (PEP-3156/tulip) framework. It depends on and reuses most
  15. parts of PyMySQL_ . *aiomysql* tries to be like awesome aiopg_ library and
  16. preserve same api, look and feel.
  17. Internally **aiomysql** is copy of PyMySQL, underlying io calls switched
  18. to async, basically ``yield from`` and ``asyncio.coroutine`` added in
  19. proper places)). `sqlalchemy` support ported from aiopg_.
  20. Documentation
  21. -------------
  22. https://aiomysql.readthedocs.io/
  23. Mailing List
  24. ------------
  25. https://groups.google.com/forum/#!forum/aio-libs
  26. Basic Example
  27. -------------
  28. **aiomysql** based on PyMySQL_ , and provides same api, you just need
  29. to use ``await conn.f()`` or ``yield from conn.f()`` instead of calling
  30. ``conn.f()`` for every method.
  31. Properties are unchanged, so ``conn.prop`` is correct as well as
  32. ``conn.prop = val``.
  33. .. code:: python
  34. import asyncio
  35. from aiomysql import create_pool
  36. loop = asyncio.get_event_loop()
  37. async def go():
  38. async with create_pool(host='127.0.0.1', port=3306,
  39. user='root', password='',
  40. db='mysql', loop=loop) as pool:
  41. async with pool.get() as conn:
  42. async with conn.cursor() as cur:
  43. await cur.execute("SELECT 42;")
  44. value = await cur.fetchone()
  45. print(value)
  46. loop.run_until_complete(go())
  47. Connection Pool
  48. ---------------
  49. Connection pooling ported from aiopg_ :
  50. .. code:: python
  51. import asyncio
  52. import aiomysql
  53. loop = asyncio.get_event_loop()
  54. @asyncio.coroutine
  55. def test_example():
  56. pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
  57. user='root', password='',
  58. db='mysql', loop=loop)
  59. with (yield from pool) as conn:
  60. cur = yield from conn.cursor()
  61. yield from cur.execute("SELECT 10")
  62. # print(cur.description)
  63. (r,) = yield from cur.fetchone()
  64. assert r == 10
  65. pool.close()
  66. yield from pool.wait_closed()
  67. loop.run_until_complete(test_example())
  68. Example of SQLAlchemy optional integration
  69. ------------------------------------------
  70. Sqlalchemy support has been ported from aiopg_ so api should be very familiar
  71. for aiopg_ user.:
  72. .. code:: python
  73. import asyncio
  74. from aiomysql.sa import create_engine
  75. import sqlalchemy as sa
  76. metadata = sa.MetaData()
  77. tbl = sa.Table('tbl', metadata,
  78. sa.Column('id', sa.Integer, primary_key=True),
  79. sa.Column('val', sa.String(255)))
  80. @asyncio.coroutine
  81. def go():
  82. engine = yield from create_engine(user='root',
  83. db='aiomysql',
  84. host='127.0.0.1',
  85. password='')
  86. with (yield from engine) as conn:
  87. yield from conn.execute(tbl.insert().values(val='abc'))
  88. res = yield from conn.execute(tbl.select())
  89. for row in res:
  90. print(row.id, row.val)
  91. asyncio.get_event_loop().run_until_complete(go())
  92. Requirements
  93. ------------
  94. * Python_ 3.3+
  95. * asyncio_ or Python_ 3.4+
  96. * PyMySQL_
  97. .. _Python: https://www.python.org
  98. .. _asyncio: http://docs.python.org/3.4/library/asyncio.html
  99. .. _aiopg: https://github.com/aio-libs/aiopg
  100. .. _PyMySQL: https://github.com/PyMySQL/PyMySQL
  101. .. _Tornado-MySQL: https://github.com/PyMySQL/Tornado-MySQL