__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. async def __aenter__(self):
  65. await self.init_pool()
  66. return self
  67. async def __aexit__(self, exc_type, exc_val, exc_tb):
  68. await self.close_pool()
  69. class TaskMySQLClient(object):
  70. """
  71. Async MySQL
  72. """
  73. def __init__(self):
  74. self.mysql_pool = None
  75. async def init_pool(self):
  76. """
  77. 初始化连接
  78. :return:
  79. """
  80. self.mysql_pool = await aiomysql.create_pool(
  81. host='rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
  82. port=3306,
  83. user='changwen_admin',
  84. password='changwen@123456',
  85. db='long_articles',
  86. charset='utf8mb4',
  87. connect_timeout=120
  88. )
  89. print("mysql init successfully")
  90. async def close_pool(self):
  91. """
  92. 关闭 mysql 连接
  93. :return:
  94. """
  95. self.mysql_pool.close()
  96. await self.mysql_pool.wait_closed()
  97. async def async_select(self, sql):
  98. """
  99. select method
  100. :param sql:
  101. :return:
  102. """
  103. async with self.mysql_pool.acquire() as conn:
  104. async with conn.cursor() as cursor:
  105. await cursor.execute(sql)
  106. result = await cursor.fetchall()
  107. return result
  108. async def async_insert(self, sql, params):
  109. """
  110. insert and update method
  111. :param params:
  112. :param sql:
  113. :return:
  114. """
  115. async with self.mysql_pool.acquire() as coon:
  116. async with coon.cursor() as cursor:
  117. await cursor.execute(sql, params)
  118. await coon.commit()