match_video_task.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. @author: luojunhui
  3. """
  4. import time
  5. import datetime
  6. import asyncio
  7. import aiomysql
  8. from tasks import MatchTask1
  9. class TaskMySQLClient(object):
  10. """
  11. Async MySQL
  12. """
  13. def __init__(self):
  14. self.mysql_pool = None
  15. async def init_pool(self):
  16. """
  17. 初始化连接
  18. :return:
  19. """
  20. self.mysql_pool = await aiomysql.create_pool(
  21. host='rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com',
  22. port=3306,
  23. user='crawler',
  24. password='crawler123456@',
  25. db='piaoquan-crawler',
  26. charset='utf8mb4',
  27. connect_timeout=120,
  28. )
  29. print("mysql init successfully")
  30. async def close_pool(self):
  31. """
  32. 关闭 mysql 连接
  33. :return:
  34. """
  35. self.mysql_pool.close()
  36. await self.mysql_pool.wait_closed()
  37. async def async_select(self, sql):
  38. """
  39. select method
  40. :param sql:
  41. :return:
  42. """
  43. async with self.mysql_pool.acquire() as conn:
  44. async with conn.cursor() as cursor:
  45. await cursor.execute(sql)
  46. result = await cursor.fetchall()
  47. return result
  48. async def async_insert(self, sql, params):
  49. """
  50. insert and update method
  51. :param params:
  52. :param sql:
  53. :return:
  54. """
  55. async with self.mysql_pool.acquire() as coon:
  56. async with coon.cursor() as cursor:
  57. await cursor.execute(sql, params)
  58. await coon.commit()
  59. async def main():
  60. """
  61. main job
  62. :return:
  63. """
  64. TMC = TaskMySQLClient()
  65. await TMC.init_pool()
  66. PD = MatchTask1(TMC)
  67. await PD.deal()
  68. if __name__ == '__main__':
  69. while True:
  70. asyncio.run(main())
  71. now_str = datetime.datetime.now().__str__()
  72. print("{} 请求执行完成, 等待10s".format(now_str))
  73. time.sleep(10)