【问题标题】:Google Endpoints does not refresh the list from previous request even if I am creating a new object?即使我正在创建新对象,Google Endpoints 也不会刷新先前请求的列表?
【发布时间】:2015-04-27 05:41:33
【问题描述】:

问题是它第一次在列表中给出 1 个值,但从下一次它返回以前的值和新值。 # 第一个请求答案:{“数字”:[“1”]} # 第二个请求答案:{ "number": [ "1", "1" ]} # 第三个请求答案:{ "number": [ "1", "1", "1" ]} # 等等 # 新对象的列表怎么会从之前的请求中获取值???

import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote

class testInput(messages.Message):
    number = messages.IntegerField(1)

class testOutput(messages.Message):
    number = messages.IntegerField(1, repeated=True)

class counter:
    count = []  
    def add(self, number):
        self.count.append(number)

@endpoints.api(name='testClass', version='v1.0')
class testClass(remote.Service):


@endpoints.method(testInput, testOutput,
                  path='countNow', http_method='GET',
                  name='countNow')

def countNow(self, request):

    #creating a new object of counter class
    counterObj = counter() # NOTE: IT SHOULD A NEW INSTANCE AND ITS ALL OBJECT MUST BE NEW
                           # e.g count list must be empty

    #getting the new number from request
    requestNumber = int(request.number)

    #creating an object of output class
    output = testOutput()

    #adding the number in list
    counterObj.add(requestNumber) # NOTE: ADDING A NUMBER IN THE LIST
                                  #HENCE: LIST SHOULD CONTAIN ONLY ONE VALUE IN IT AND ITS LENGTH MUST BE: 1

    #storing the list in output
    output.number = counterObj.count

    #returning output
    return output #RETURNING THE LIST AND IT SHOULD RETURN ONLY SINGLE VALUE IN LIST



application = endpoints.api_server([testClass])

【问题讨论】:

    标签: python google-app-engine google-cloud-endpoints


    【解决方案1】:

    试试这个,需要构造函数__init__重新初始化。

    class counter:
    
        def __init__(self):
            self.count = []    # instance variable unique to each instance
    
        def add(self, number):
            self.count.append(number)
    

    你的情况

    class counter:
        count = []
    

    这个变量count类变量将被所有实例共享

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2021-12-27
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2016-12-27
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多