arrays - Python/Tornado Class wrapper caching issue -


i'm implementing wrapper around python's array data structure. i'm doing practical reasons in application, example code provided reproduce problem. array doesn't seem 'cleared' each request through tornado abstraction.

if don't use array abstraction, there no problem. leads me believe there bug somewhere in cpython implementation.

from tornado import websocket, web, ioloop import json  class array():     _data = []     def push(self, value):         self._data.append(value)      def json(self):         return json.dumps(self._data)   class clienthandler(web.requesthandler):     def prepare(self):         self.set_header("content-type", "application/json")      def get(self):         array = array()          in range(0, 6):             array.push({'id': i})          self.write(array.json())         self.finish()  app = web.application([     (r'/client', clienthandler), ], debug=true)  if __name__ == '__main__':     kwargs = {"address": "127.0.0.1"}     app.listen(port=8888, **kwargs)     ioloop.ioloop.instance().start() 

the output after refreshing page once i've started python process follows in sequence:

sequence 1

[{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}] 

sequence 2

[{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}, {"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}] 

sequence 3

[{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}, {"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}, {"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}] 

this response out not expected output. expected output should have length of 6 of json output array. problem not happen if don't wrap python's data structure.

why happen? i'm new enthusiastic python user, type of thing discourages me using language if can't handle simple abstraction.

extra

to run this:

  • install tornado package @ pip install tornado,
  • save code provided in file called app.py
  • execute python app.py
  • open web application in broswer http://127.0.0.1/client

the issue because array._data static member of array meaning it's value same on instances of array.

class array():     _data = []     def push(self, value):         self._data.append(value)      def json(self):         return json.dumps(self._data) 

to solve problem, make _data instance member.

class array():     def __init__(self):         self._data = []      def push(self, value):         self._data.append(value)      def json(self):         return json.dumps(self._data) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -