1

I have this same code which I have mentioned in this question of mine. Now I have hosted the same web.py app on Apache. But when I start Apache the code inside if __name__ == "__main__": is not executed.

Is it possible to run backgroud process(check the other question for the code) when hosted in Apache?

Why does the code inside if __name__ == "__main__": is not executed?

This is working well when web.py is run without Apache.

Community
  • 1
  • 1
Marlon Abeykoon
  • 11,927
  • 4
  • 54
  • 75
  • This issue is discussed in http://modwsgi.readthedocs.io/en/develop/user-guides/assorted-tips-and-tricks.html You can in the WSGI script file (only), use ``__name__.startswith('_mod_wsgi_')``. – Graham Dumpleton Feb 01 '17 at 17:48

1 Answers1

2

Code inside if __name__ == '__main__': doesn't run because that's not how Apache runs python code.

More likely, you're running your python under mod_wsgi or uwsgi, which is a way to have Apache talk to python.

Keep the if __name__ == '__main__': stuff: that's useful for simple testing, but add a similar block like:

if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()
elif under_mod_wsgi or under_uwsgi:
    app = web.application(urls, globals())
    application = app.wsgifunc()  # !!rather than app.run()

Your Process stuff should still run (reference your other question).

To detect if under_mod_wsgi you can:

try:
    from mod_wsgi import version
    if version:
        pass
    under_mod_wsgi = True
except ImportError:
    under_mod_wsgi = False

try:
    import uwsgi
    under_uwsgi = True
except ImportError:
    under_uwsgi = False
Marlon Abeykoon
  • 11,927
  • 4
  • 54
  • 75
pbuck
  • 4,291
  • 2
  • 24
  • 36