__init__.py 3.1 KB

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