matchVideoFromHistoryArticleDESC.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. @author: luojunhui
  3. """
  4. import datetime
  5. import aiomysql
  6. import asyncio
  7. from tasks import MatchTask4
  8. class TaskMySQLClient(object):
  9. """
  10. Async MySQL
  11. """
  12. def __init__(self):
  13. self.mysql_pool = None
  14. async def init_pool(self):
  15. """
  16. 初始化连接
  17. :return:
  18. """
  19. self.mysql_pool = await aiomysql.create_pool(
  20. host='rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com',
  21. port=3306,
  22. user='crawler',
  23. password='crawler123456@',
  24. db='piaoquan-crawler',
  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. await cursor.execute(sql, params)
  57. await coon.commit()
  58. async def main():
  59. """
  60. main2
  61. :return:
  62. """
  63. TMC = TaskMySQLClient()
  64. await TMC.init_pool()
  65. PD = MatchTask4(TMC)
  66. await PD.deal()
  67. now_str = datetime.datetime.now().__str__()
  68. print("{} 请求执行完成, 等待10s".format(now_str))
  69. await asyncio.sleep(10)
  70. if __name__ == '__main__':
  71. while True:
  72. asyncio.run(main())