import time import asyncio from sqlor.dbpools import DBPools from appPublic.worker import AsyncWorker from appPublic.httpclient import HttpClient dbs = { "iptvdb":{ "driver":"aiomysql", "async_mode":True, "coding":"utf8", "dbname":"iptvdb", "maxconn":100, "kwargs":{ "user":"test", "db":"iptvdb", "password":"QUZVcXg5V1p1STMybG5Ia6mX9D0v7+g=", "host":"localhost" } } } async def check_availble(rec): url = rec['url'] print(f'handle {rec["id"]}, {url}...') t =time.time() r = None try: hc = HttpClient() r = await hc.get(url) await hc.close() except: pass if r is None: db = DBPools() d = { "id":rec['id'] } print('sql run ... ', d) async with db.sqlorContext('iptvdb') as sor: await sor.sqlExe("update iptvchannels set del_flg='1' where id=${id}$", d) t1 = time.time() print(f'{rec["id"]} deleted cost {t1 - t} seconds') return t1 = time.time() print(f'{url} ok {t1 - t} seconds') async def main(): db = DBPools(dbs) sql = "select * from iptvchannels where del_flg='0'" recs = [] async with db.sqlorContext('iptvdb') as sor: recs = await sor.sqlExe(sql, {}) aw = AsyncWorker(maxtask=10) g = [asyncio.create_task(aw(check_availble, r)) for r in recs ] await asyncio.wait(g) if __name__ == '__main__': asyncio.get_event_loop().run_until_complete(main())