更行代码

main
周平 1 year ago
parent 6931e7914e
commit a034aa4bf6

@ -1,31 +1,30 @@
import itertools
import contextlib import contextlib
import logging import itertools
from contextlib import asynccontextmanager
from typing import List, Any, Optional from typing import List, Any, Optional
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from contextlib import asynccontextmanager from sqlalchemy.orm import sessionmaker
from website import settings from website import settings
from website.handler import Row
class Row(dict): # class Row(dict):
"""A dict that allows for object-like property access syntax.""" # """A dict that allows for object-like property access syntax."""
#
def __getattr__(self, name): # def __getattr__(self, name):
try: # try:
return self[name] # return self[name]
except KeyError: # except KeyError:
raise AttributeError(name) # raise AttributeError(name)
def dict_to_obj(d): def dict_to_obj(d):
return type('GenericClass', (object,), d)() return type('GenericClass', (object,), d)()
def to_json_list(cursor: Any) -> Optional[List[Row]]: def to_json_list(cursor: Any) -> Optional[List[Row]]:
column_names = list(cursor.keys()) column_names = list(cursor.keys())
result = cursor.fetchall() result = cursor.fetchall()
@ -59,7 +58,9 @@ app_engine = create_engine(
pool_recycle=int(settings.SQLALCHEMY_POOL_RECYCLE), # 多久时间回收连接 pool_recycle=int(settings.SQLALCHEMY_POOL_RECYCLE), # 多久时间回收连接
) )
Session = sessionmaker(bind=app_engine) Session = sessionmaker(bind=app_engine, expire_on_commit=False)
# Base = declarative_base(app_engine) # Base = declarative_base(app_engine)

@ -86,6 +86,7 @@ class EntityIndexHandler(APIHandler):
# model_counts = [result[0] for result in results] # model_counts = [result[0] for result in results]
# device_counts = [result[1] for result in results] # device_counts = [result[1] for result in results]
# *用于解包参数列表将列表中的每个元素作为单独的参数传递给asyncio.gather函数
count_results = await asyncio.gather( count_results = await asyncio.gather(
*[ *[
enterprise.get_enterprise_model_and_device_count(entity_id=item["id"]) enterprise.get_enterprise_model_and_device_count(entity_id=item["id"])

Loading…
Cancel
Save