ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

String Concatenation Python 2.5(使用cStringIO)

2019-07-14 03:55:54  阅读:247  来源: 互联网

标签:python string-concatenation concatenation google-app-engine python-2-5


我正在使用Google Appengine和Python 2.5,我有一个导致瓶颈的功能.我传递了一个从数据存储区检索到的200个模型实例的列表,然后以json格式返回它,然后传递给客户端.

我最初使用=将所有值连接在一起,但服务器用JSON响应大约需要30秒.我运行了一些检查和代码,然后在一秒钟内运行此函数.这是服务器响应JSON之前的最后一条语句,以及在1秒内(在我的本地网络上)达到客户端平均值所需的时间.此功能平均需要30秒才能执行.

我读了这个article,并尝试使用cStringIO方法(我也使用了列表连接方法,但它花了相同的时间,cStringIO使用更少的内存,所以我坚持使用它).但是,这与=连接(有时更长)的时间大致相同.任何人都可以看到我的代码有任何问题,可能会使它变慢?

编辑:老板说它必须这样做.没有json图书馆(与他一起接受).

编辑2:LastName模型:

class LastName(db.Model): 
    entry = db.ReferenceProperty(AlumniEntry, collection_name='last_names') 
    last_name = db.StringProperty(indexed=False) 
    last_name_search = db.StringProperty()

AlumniEntry是被查询的模型.我将从ds返回的列表传递给get_json_from_alumnus()(alumnus参数).

def get_json_from_alumnus(alumnus, search, total=0):
    if len(alumnus) > 0:
        from cStringIO import StringIO
        concat_file = StringIO()

        concat_file.write('{ "alumnus": [')
        i = 0
        for alumni in alumnus:
            if alumni.author:
                author = alumni.author.nickname()
            else:
                author = 'Anonymous'

            concat_file.write('{ ')
            concat_file.write('"author": "')
            concat_file.write(author)
            concat_file.write('", ')
            concat_file.write('"title": "')
            concat_file.write(alumni.title)
            concat_file.write('", ')
            concat_file.write('"first_name": "')
            concat_file.write(alumni.first_name)
            concat_file.write('", ')

            concat_file.write(' "last_names": [')
            j = 0
            for lname in alumni.last_names:
                concat_file.write('{ "last_name": "')
                concat_file.write('lname.last_name')
                concat_file.write('" }')
                if not j == alumni.last_names.count() - 1:
                    #last_names += ','
                    concat_file.write(',')
                j +=1
            concat_file.write('], ')

            concat_file.write(' "addresses": [')
            j = 0
            for address in alumni.addresses:
                if address.street == '' and address.city == '' and address.state == '' and address.zip_code == '':
                    break

                concat_file.write('{ "address":{ "street" : "')
                concat_file.write(address.street)
                concat_file.write('", ')
                concat_file.write('"city" : "')
                concat_file.write(address.city)
                concat_file.write('", ')
                concat_file.write('"state" : "')
                concat_file.write(address.state)
                concat_file.write('", ')
                concat_file.write('"zip_code" : "')
                concat_file.write(address.zip_code)
                concat_file.write('" } }')

                if not j == alumni.addresses.count() - 1:
                    concat_file.write(',')
                j += 1
            concat_file.write('], ')

            concat_file.write(' "numbers": [')
            j = 0
            for phone_number in alumni.phone_numbers:
                concat_file.write('{ "phone_number": "')
                concat_file.write(phone_number.number)
                concat_file.write('" }')
                if not j == alumni.phone_numbers.count() - 1:
                    concat_file.write(',')
                j += 1
            concat_file.write('], ')

            concat_file.write(' "emails": [')
            j = 0
            for email in alumni.emails:
                concat_file.write('{ "email": "')
                concat_file.write(email.email)
                concat_file.write('" }')
                if not j == alumni.emails.count() - 1:
                    concat_file.write(',')
                j += 1
            concat_file.write('], ')

            concat_file.write('"grad_year": "')
            concat_file.write(alumni.grad_year)
            concat_file.write('", ')
            concat_file.write('"elementary": "')
            concat_file.write(alumni.elementary)
            concat_file.write('", ')
            concat_file.write('"entered": "')
            concat_file.write(str(alumni.entered.strftime('%B %d %Y')))
            concat_file.write('", ')
            concat_file.write('"key": "')
            concat_file.write(str(alumni.key()))
            concat_file.write('" ')
            concat_file.write('}')

            if not  i == len(alumnus) - 1:
                concat_file.write(',')
            i += 1
        concat_file.write('], "total" : "')
        concat_file.write(str(total))
        concat_file.write('" }')
    else:
        concat_file.write('{ "alumnus": "No Alumni Entered Yet!" }' if not search else '{ "alumnus": "No Matches!" }')

    return concat_file.getvalue()

解决方法:

我怀疑你的代码中有这一行:

if not j == alumni.last_names.count() - 1:

(和一些类似的线).

你没有发布你的模型,但我看起来像alumni.last_names可能是一个查询?为每个实体运行查询将是一个非常糟糕的主意,并且可能很好地支配您的成本.使用cStringIO连接几千个字符串不应该花费大约30秒.

使用Appstats:http://code.google.com/appengine/docs/python/tools/appstats.html(您甚至可以在开发应用程序服务器中尝试此操作)可以很容易地找出您是否正在执行太多查询.

PS.单数实际上是校友,复数是校友.

标签:python,string-concatenation,concatenation,google-app-engine,python-2-5
来源: https://codeday.me/bug/20190714/1455875.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有