I have a large XML file that opens, loads into memory, and then closes with the Python class. A simplified example would look like this:
class Dictionary(): def __init__(self, filename): f = open(filename) self.contents = f.readlines() f.close() def getDefinitionForWord(self, word):
And in my Flask application:
from dictionary import Dictionary dictionary = Dictionary('dictionary.xml') print 'dictionary object created' @app.route('/') def home(): word = dictionary.getDefinitionForWord('help')
I understand that in an ideal world, I would use a database instead of XML and create a new connection to this database for each request.
In the documents, I realized that the application context in the jar means that each request will recreate dictionary = new Dictionary('dictionary.xml') by opening the file on disk and re-reading all this into memory. However, when I look at the debug output, I see a dictionary object created line printed exactly once, despite being connected from several sources (different sessions?).
My first question is:
It seems to me that the application only downloads the XML file once ... Then I can assume that it is in memory all over the world and can be safely read by a large number of simultaneous requests, limited only by RAM on my server - right? If XML is 50 MB, then approx. 50 MB in memory and serviced until simultaneous requests at high speed ... I assume that it is not so simple.
And my second question:
If this is not the case, what limits am I going to hit with my ability to handle large volumes of traffic? How many requests can I process if I have 50 MB of XML that opens multiple times, reads from disk, and closes? I guess one at a time.
I understand that this is vague and hardware dependent, but I'm new to Flask, python and programming for the web, and just looking for guidance.
Thanks!