这是我正在使用的示例代码。我想快速解析页面并将结果数据输入数据库。但是,添加一行后,我的代码开始运行明显变慢。我了解这与数据库的工作有关。但我不明白如何解决它。
如果您对加速此代码有任何其他建议,我将不胜感激。
import asyncpg
import asyncio
import aiohttp
from settings import URI, SQL, URLS
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class DBManager(metaclass=Singleton):
""" Class for interacting with Postgres database """
def __init__(self, dsn) -> None:
""" The constructor takes the database """
self.dsn = dsn
self.pool = None
async def connect(self):
self.pool = await asyncpg.create_pool(dsn=self.dsn)
async def insert_data(self, peson: str, address: str):
async with self.pool.acquire() as connect:
return await connect.execute(SQL, type_tx, address)
db = DBManager(URI)
async def check_address(url, session):
async with session.get(url) as result:
try:
result = await result.json()
person = 'adult' if result['age'] >= 21 else 'child'
address = result['address']
await db.insert_data(person, address)
return print(address, person)
except Exception as e:
print(e)
async def bound_fetch(sem, url, session):
async with sem:
return await check_address(url, session)
async def main():
await db.connect()
urls = [url for i in URLS]
sem = asyncio.Semaphore(50)
tasks = []
async with aiohttp.ClientSession() as session:
for url in urls:
task = asyncio.ensure_future(bound_fetch(sem, url, session))
tasks.append(task)
responses = asyncio.gather(*tasks)
await responses
if __name__ == '__main__':
asyncio.run(main())