va_task.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. @author: luojunhui
  3. """
  4. import asyncio
  5. import time
  6. import aiomysql
  7. from deal import MatchArticlesTask
  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 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):
  48. """
  49. insert and update method
  50. :param sql:
  51. :return:
  52. """
  53. async with self.mysql_pool.acquire() as coon:
  54. async with coon.cursor() as cursor:
  55. await cursor.execute(sql)
  56. await coon.commit()
  57. async def main():
  58. """
  59. main --version 01
  60. :return:
  61. """
  62. TMC = TaskMySQLClient()
  63. await TMC.init_pool()
  64. MAT = MatchArticlesTask(mysql_client=TMC)
  65. await MAT.whisper_task()
  66. await asyncio.sleep(120)
  67. await MAT.materials_task()
  68. await asyncio.sleep(120)
  69. await MAT.ai_task()
  70. await asyncio.sleep(120)
  71. if __name__ == '__main__':
  72. while True:
  73. asyncio.run(main())
  74. print("执行完成,等待120s")
  75. time.sleep(120)