query = Child.query
query = query.with_entities(func.max(Child.id))
query = query.group_by(Child.parent_id)
subquery = query.subquery()
query = Child.query
query = query.filter(Child.id.in_(subquery))
query = query.order_by(Child.id.desc())
childs = query.all()
class Obj(Model):
id = Column(UUID, primary_key=True)
class XmlHandler(ContentHandler):
def __init__(self):
pass
def startElement(self, name, attrs):
if name == 'Objects':
print('start parse')
elif name == 'Object':
aoid = attrs['ID']
self.obj = Obj.query.filter_by(id=id).first() # это 2-ое узкое место
if not self.obj:
self.obj = Obj()
def endElement(self, name):
if name == 'Objects':
print('end')
elif name == 'Object':
self.obj.save()
gc.collect() # это было 1-ое узкое место
xml.sax.parse(xml_path, XmlHandler())
# -*- coding: utf-8 -*-
"""Create an application instance."""
from flask.helpers import get_debug_flag
from myapp.app import create_app
from myapp.settings import DevConfig, ProdConfig
from myapp.extensions import celery
from myapp.utils import init_celery
CONFIG = DevConfig if get_debug_flag() else ProdConfig
app = create_app(CONFIG)
init_celery(app, celery)
def init_celery(app, celery):
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
celery worker --app=autoapp.celery --loglevel=debug