将目录中的文件提供给uWSGI Flask应用程序与Emperor一起运行

时间:2016-02-04 05:52:00

标签: python flask uwsgi

我将uWSGI设置更改为使用Emperor进行多应用程序部署。在加载mypasswords.pwds'时使用旧设置在我的应用旁边保存的文件,没有问题。

现在我和皇帝一起运行所有的东西'没有这样的文件或目标'错误。这可能是因为我的设置或因为皇帝只是以不同的方式工作而且我不确定是什么情况。

目录树:

Project
    thisismyapp.py
    passwords.pwds

配置uWSGI Emperor:

[uwsgi]
# Variables
base = /var/www/Project

# Generic Config
home = %(base)/venv
pythonpath = %(base)
socket = /tmp/%n.sock
module = thisismyapp:app # Is this an issue?
logto = /var/log/uwsgi/%n.log

回溯:

*** Starting uWSGI 2.0.11.2 (64bit) on [Thu Feb  4 00:41:47 2016] ***
compiled with version: 4.9.2 on 24 October 2015 23:42:32
os: Linux-3.19.0-42-generic #48-Ubuntu SMP Thu Dec 17 22:54:45 UTC 2015
nodename: website
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/apps-enabled
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 1868
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/project.sock fd 3
Python version: 2.7.9 (default, Apr  2 2015, 15:37:21)  [GCC 4.9.2]
Set PythonHome to /var/www/Project/venv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x17355e0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145520 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
added /var/www/Project/ to pythonpath.
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x17355e0 pid: 6772 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 6772)
spawned uWSGI worker 1 (pid: 6779, cores: 1)
[pid: 6779|app: 0|req: 1/1] 79.227.145.119 () {46 vars in 967 bytes} [Thu Feb  4 00:41:51 2016] GET / => generated 3552 bytes in 355 msecs (HTTP/1.1 200) 2 headers in 81 bytes (1 switches on core 0)
announcing my loyalty to the Emperor...
[Errno 2] No such file or directory: 'passwords.pwds'

Python文件访问:

f = open('passwords.pwds', 'r')

1 个答案:

答案 0 :(得分:0)

试试这个:

from os import path

passwords_location = path.join(path.dirname(path.realpath(__file__), 'passwords.pwds'))
with open(passwords_location) as f:
    do_something()

使用相对位置的原始方式,如果您不在“项目”文件夹中,可能无法找到该文件。另外,使用with块是处理文件的更好方法,它将确保在退出上下文块时关闭文件。