__name__ 变量等于 “wsgi” 而不是 “__main__”

huangapple go评论142阅读模式
英文:

__name__ variable equals "wsgi" instead of "__main__"

问题

I have wsgi file, where there is a line:

wsgi.py

if __name__ == "main":
    app.run()

But today my server didn't start and when I printed __name__, I saw that it equals "wsgi" for no reason. I start my server like "sudo systemctl restart myserver.service"

myserver.ini

[uwsgi]
module = wsgi:app

master = true
processes = 3

socket = /var/www/myserver/myserver.sock
chmod-socket = 660
vacuum = true

die-on-term = true
logto = /var/www/myserver/server.log
enable-threads = true

myserver.service

[Unit]
Description=uWSGI instance to serve myserver
After=network.target

[Service]
User=savvasenok
Group=www-data
WorkingDirectory=/var/www/myserver
Environment="PATH=/home/savvasenok/MyServerEnv/bin"
ExecStart=/home/savvasenok/MyServerEnv/bin/uwsgi --ini /var/www/myserver/myserver.ini

[Install]
WantedBy=multi-user.target
英文:

I have wsgi file, where there is a line:

wsgi.py

if __name__ == "__main__":
    app.run()

But today my server didn't start and when I printed __name__, saw that it equals "wsgi" for no reason. I start my server like "sudo systemctl restart myserver.service"

myserver.ini

module = wsgi:app

master = true
processes = 3

socket = /var/www/myserver/myserver.sock
chmod-socket = 660
vacuum = true

die-on-term = true
logto = /var/www/myserver/server.log
enable-threads = true

myserver.service

Description=uWSGI instance to serve myserver
After=network.target

[Service]
User=savvasenok
Group=www-data
WorkingDirectory=/var/www/myserver
Environment="PATH=/home/savvasenok/MyServerEnv/bin"
ExecStart=/home/savvasenok/MyServerEnv/bin/uwsgi --ini /var/www/myserver/myserver.ini

[Install]
WantedBy=multi-user.target

答案1

得分: 1

根据这里的解释:

> 如果源文件作为主程序执行,解释器会将__name__变量设置为“__main__”的值。如果此文件被从另一个模块导入,__name__将被设置为该模块的名称。

所以原因是您正在运行一个导入wsgi的Python模块(或文件)。在您的情况下,我认为您将wsgi导入到/home/savvasenok/MyServerEnv/bin/uwsgi模块中,并将其设置为ExecStart,然后执行该模块。

您还可以查看这个答案以获取更多信息。

英文:

As it explains in here:

> If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value “__main__”. If this file is being imported from another module, name will be set to the module’s name.

So the reason is that you are running a python module (or file) which imports wsgi. In your case I think you imported wsgi to the /home/savvasenok/MyServerEnv/bin/uwsgi module which you sat as ExecStart and is being executed.

You can also check this answer for more information.

huangapple
  • 本文由 发表于 2020年1月7日 00:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定