__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """
  2. @author: luojunhui
  3. """
  4. import aiomysql
  5. class AsyncMySQLClient(object):
  6. """
  7. 异步 mysql 连接池
  8. """
  9. def __init__(self, app=None):
  10. if not app:
  11. self.mysql_pool = None
  12. else:
  13. self.mysql_pool = app
  14. async def init_pool(self):
  15. """
  16. 初始化连接
  17. :return:
  18. """
  19. self.mysql_pool = await aiomysql.create_pool(
  20. host='rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
  21. port=3306,
  22. user='changwen_admin',
  23. password='changwen@123456',
  24. db='long_articles',
  25. charset='utf8mb4',
  26. connect_timeout=120,
  27. )
  28. print("mysql init successfully")
  29. async def close_pool(self):
  30. """
  31. 关闭 mysql 连接
  32. :return:
  33. """
  34. self.mysql_pool.close()
  35. await self.mysql_pool.wait_closed()
  36. async def async_select(self, sql):
  37. """
  38. select method
  39. :param sql:
  40. :return:
  41. """
  42. async with self.mysql_pool.acquire() as conn:
  43. async with conn.cursor() as cursor:
  44. await cursor.execute(sql)
  45. result = await cursor.fetchall()
  46. return result
  47. async def async_insert(self, sql, params):
  48. """
  49. insert and update method
  50. :param params:
  51. :param sql:
  52. :return:
  53. """
  54. async with self.mysql_pool.acquire() as coon:
  55. async with coon.cursor() as cursor:
  56. try:
  57. await cursor.execute(sql, params)
  58. affected_rows = cursor.rowcount
  59. await coon.commit()
  60. return affected_rows
  61. except Exception as e:
  62. await coon.rollback()
  63. raise
  64. class TaskMySQLClient(object):
  65. """
  66. Async MySQL
  67. """
  68. def __init__(self):
  69. self.mysql_pool = None
  70. async def init_pool(self):
  71. """
  72. 初始化连接
  73. :return:
  74. """
  75. self.mysql_pool = await aiomysql.create_pool(
  76. host='rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
  77. port=3306,
  78. user='changwen_admin',
  79. password='changwen@123456',
  80. db='long_articles',
  81. charset='utf8mb4',
  82. connect_timeout=120
  83. )
  84. print("mysql init successfully")
  85. async def close_pool(self):
  86. """
  87. 关闭 mysql 连接
  88. :return:
  89. """
  90. self.mysql_pool.close()
  91. await self.mysql_pool.wait_closed()
  92. async def async_select(self, sql):
  93. """
  94. select method
  95. :param sql:
  96. :return:
  97. """
  98. async with self.mysql_pool.acquire() as conn:
  99. async with conn.cursor() as cursor:
  100. await cursor.execute(sql)
  101. result = await cursor.fetchall()
  102. return result
  103. async def async_insert(self, sql, params):
  104. """
  105. insert and update method
  106. :param params:
  107. :param sql:
  108. :return:
  109. """
  110. async with self.mysql_pool.acquire() as coon:
  111. async with coon.cursor() as cursor:
  112. await cursor.execute(sql, params)
  113. await coon.commit()