【发布时间】:2013-02-15 21:31:14
【问题描述】:
我有这个来自/home/myname/myapp/app.py:
from flask import Flask
app = Flask(__name__)
print __name__
@app.route('/')
def index():
return "Hello world!"
if __name__ == '__main__':
print 'in if'
app.run()
当我跑步时:
$ gunicorn app:app -b 127.0.0.2:8000
上面写着:
2013-03-01 11:26:56 [21907] [INFO] Starting gunicorn 0.17.2
2013-03-01 11:26:56 [21907] [INFO] Listening at: http://127.0.0.2:8000 (21907)
2013-03-01 11:26:56 [21907] [INFO] Using worker: sync
2013-03-01 11:26:56 [21912] [INFO] Booting worker with pid: 21912
app
所以应用程序的__name__ 是app。不像__main__ 那样我需要它来运行 if 语句。
我尝试在目录中放置一个空的__init__.py。这是我的nginx sites-enabled default:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
root /home/myname/myapp;
# Make site accessible from http://localhost/
server_name localhost;
location / {
proxy_pass http://127.0.0.2:8000;
}
}
编辑
...当我访问该网站时,此应用确实打印'Hello world'。关键是我需要__name__ 等于'__main__'。我也只是想知道为什么它不等于__main__。
Edit 2
... 我只是顿悟了,我不需要运行app.run(),因为这就是 Gunicorn 的用途。呃。但我仍然想弄清楚为什么__name__ 不是'__main__'
【问题讨论】:
-
你为什么需要它?当您使用
guincorn运行它时,请尝试访问http://127.0.0.2:8000/。
标签: python nginx flask gunicorn