File upload only once during python script initialization using Mod_WSGI and Bottle

I am new to Python, Mod_WSGI and Bottle. My main problem is that when the process starts using Mod_WSGI, I want it to download the file once during initialization. When running a terminal script, you simply useif __name__ == '__main__'

I need to download the file once during initialization (or the first call) so that any subsequent calls to the process do not require a file reload. I am not sure how to do this.

The following code runs whenever someone goes to the recommendations page

@route('/recommend')
def recommend():
    parser = OptionParser(usage="usage: %prog [options]")
    parser.add_option('-f', '--file', default='data.csv', help='Specify csv file to read item data from.')
    parser.add_option('-D', '--debug', action='store_true', dest='debug', help='Put bottle in debug mode.')
    (options, args) = parser.parse_args()
    return res.recommend(request)

How do I make the first 4 lines (those that contain the parser) only during initialization, so I just need to call res.recommend () whenever the recommendations page is available?

,

+3
3

WSGI script. . , .

WSGI script, , . , , , script WSGIImportScript, .

:

http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIImportScript

, / WSGI, -, WSGIProcessGroup/WSGIApplicationGroup.

+2

Python .

.

mod.py:

x = 10
print(x)

main.py:

import mod #will print 10
mod.x = 5
import mod #nothing is printed. mod.x == 5
0

, , .

:

datacache = None

@route("/someroute")
def someroute():
    if not datacache:
        datacache = do_something_clever_with_file(open("filename"))
    page = make_page_from_data(datacache)
    return page

, script - - . .

, .

, , , - memoization .

0

All Articles