WebFaction
Community site: login faq
0
1

So here's an odd problem and an odd, probably bad, solution:
I'm experimenting with a Flask application set up like this:

   webapps/myapp
    | --apache2
    | --htdocs
        | --__init__.py
        | --index.py
        | --myapp.py
        | --templates
            | --testTemplate.html

per what I had read here, my index.py just said:

    from myapp import app as application

and myapp.py contained all the logic for dispatch etc.:

    import sys
    import sqlite3
    from flask import Flask, request, session, g, redirect, url_for, abort, \
         render_template, flash, _app_ctx_stack

    app = Flask(__name__)
    app.debug = True

    @app.route('/')
    def hello_world():
        return 'Hello World!'

etc.

Changes to templates were reflected immediately in responses. But if I changed code in myapp.py, I had to re-save, or touch index.py before responses would reflect the changes. Very inconvenient for development purposes.

I messed about quite a bit with httpd.conf, thinking it had something to do with WSGIDaemonProcess and suchlike, but to no avail.

What I finally wound up doing strikes me as faintly ridiculous, and possibly harmful, though it works and can be removed for production. I revised index.py as follows:

    import os    
    os.utime("/home/full/path/to/myapp/htdocs/index.py", None)    
    from myapp import app as application

There's a great deal I don't understand about how a wsgi app under flask works in the context of my webfaction environment. Can anyone tell me why I get this behavior; and, is there some more canonical way to fix it than calling an os process like this?

asked 28 Jan '13, 15:55

jjon
3124
accept rate: 0%

Argh! Sorry for the noise. In my initial search I missed this, where @seanf helpfully explains about mod_wsgi in daemon mode. I'd still like to know, however, if using os.utime(), as I have here, has a downside that I haven't thought of.

(29 Jan '13, 16:21) jjon

Touching your WSGI script is the canonical way to reload code when running under mod_wsgi - this is documented in the official mod_wsgi documentation: Reloading In Daemon Mode.

Touching your WSGI script via os.utime() every time it is requested is a creative solution, but seems like it has the potential to cause issues, like some endless loop of reloading. If it doesn't have that problem in practice, then by all means, go ahead and use it.

permanent link

answered 29 Jan '13, 16:20

seanf
12.2k31836
accept rate: 37%

Thank you @seanf, that's exactly what I wanted to know. Thanks too for the link to the appropriate docs. It seemed to me too that it might cause problems down the road. So far, however, it's caused no trouble, and it will only be necessary for the development phase where I do a lot (way too much) of trial and error.

(29 Jan '13, 17:47) jjon

Every time you change index.py or any .py file, you have to restart your HTTP server: navigate to /apache2/bin then "./restart"

permanent link

answered 28 Jan '13, 16:46

execute
726
accept rate: 0%

Well, actually, no. That's what I thought too when I started, but I found restarting apache every time I changed my python code really annoying. I discovered that since index.py script merely imports myapp, I just had to re-save or touch index.py to have any changes in myapp.py reflected in the response. So, I execute os.utime() each time index.py is requested.

As I said, this does in fact work, but it seems weird and klugy to me. I was hoping someone could illuminate WHY it works, and whether or not there's a better, or more canonical way to do the same thing.

(29 Jan '13, 16:05) jjon
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×62
×44
×28
×10

question asked: 28 Jan '13, 15:55

question was seen: 3,874 times

last updated: 29 Jan '13, 17:47

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM