__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 conn:
  55. async with conn.cursor() as cursor:
  56. try:
  57. await cursor.execute(sql, params)
  58. affected_rows = cursor.rowcount
  59. await conn.commit()
  60. return affected_rows
  61. except Exception as e:
  62. await conn.rollback()
  63. raise
  64. async def async_insert_many(self, sql, params_list):
  65. """
  66. :param sql:
  67. :param params_list:
  68. :return:
  69. """
  70. async with self.mysql_pool.acquire() as conn:
  71. async with conn.cursor() as cursor:
  72. try:
  73. await cursor.executemany(sql, params_list)
  74. affected_rows = cursor.rowcount
  75. await conn.commit()
  76. return affected_rows
  77. except Exception as e:
  78. await conn.rollback()
  79. raise
  80. async def __aenter__(self):
  81. await self.init_pool()
  82. return self
  83. async def __aexit__(self, exc_type, exc_val, exc_tb):
  84. await self.close_pool()
  85. class TaskMySQLClient(object):
  86. """
  87. Async MySQL
  88. """
  89. def __init__(self):
  90. self.mysql_pool = None
  91. async def init_pool(self):
  92. """
  93. 初始化连接
  94. :return:
  95. """
  96. self.mysql_pool = await aiomysql.create_pool(
  97. host='rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
  98. port=3306,
  99. user='changwen_admin',
  100. password='changwen@123456',
  101. db='long_articles',
  102. charset='utf8mb4',
  103. connect_timeout=120
  104. )
  105. print("mysql init successfully")
  106. async def close_pool(self):
  107. """
  108. 关闭 mysql 连接
  109. :return:
  110. """
  111. self.mysql_pool.close()
  112. await self.mysql_pool.wait_closed()
  113. async def async_select(self, sql):
  114. """
  115. select method
  116. :param sql:
  117. :return:
  118. """
  119. async with self.mysql_pool.acquire() as conn:
  120. async with conn.cursor() as cursor:
  121. await cursor.execute(sql)
  122. result = await cursor.fetchall()
  123. return result
  124. async def async_insert(self, sql, params):
  125. """
  126. insert and update method
  127. :param params:
  128. :param sql:
  129. :return:
  130. """
  131. async with self.mysql_pool.acquire() as conn:
  132. async with conn.cursor() as cursor:
  133. await cursor.execute(sql, params)
  134. await conn.commit()