import aiohttp import asyncio from concurrent.futures import ThreadPoolExecutor, as_completed import traceback import time
classT: def__init__(self): self._session = None
asyncdefget(self): try: if self._session isNone: self._session = aiohttp.ClientSession() for i in range(100): asyncwith self._session.get("http://www.baidu.com") as r: t = await r.text() print(1) except Exception as e: print(traceback.format_exc()) finally: await self._session.close()
defmain(): test = T() loop = asyncio.get_event_loop() loop.run_until_complete(test.get())
import aiohttp import asyncio from concurrent.futures import ThreadPoolExecutor, as_completed import traceback import time
classT: def__init__(self): self._session = None
asyncdefget(self): try: if self._session isNoneor self._session.closed: self._session = aiohttp.ClientSession() for i in range(10): asyncwith self._session.get("http://www.baidu.com") as r: t = await r.text() print(1) except Exception as e: print(traceback.format_exc()) finally: await self._session.close()
defmain(): test = T() loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.ensure_future(test.get())) print(2) loop.run_until_complete(asyncio.ensure_future(test.get()))
import aiohttp import asyncio from concurrent.futures import ThreadPoolExecutor, as_completed import traceback import time
classT: def__init__(self): self._session = None
asyncdefget(self): try: if self._session isNoneor self._session.closed: self._session = aiohttp.ClientSession() for i in range(10): asyncwith self._session.get("http://www.baidu.com") as r: t = await r.text() print(1) except Exception as e: print(traceback.format_exc()) finally: await self._session.close()