4

Python3.8 有一个新功能,您可以将PYTHONPYCACHEPREFIX环境变量设置为 dir 路径,以便 pycache 使用单独的并行文件系统树,而不是__pycache__每个源目录中的默认子目录:

https://docs.python.org/3/whatsnew/3.8.html#parallel-filesystem-cache-for-compiled-bytecode-files

我已经设置了这个环境变量:

export PYTHONPYCACHEPREFIX="$HOME/.cache/pycache/"

这直接通过命令行与 python3.8 一起工作,但它不适用于 python3.8 WSGI Web 应用程序。您如何使用 WSGI Web 应用程序来实现它?这是我的 WSGI Web 应用程序的示例:

 # apache.conf Load python3.8 mod_wsgi (installed mod_wsgi by Python3.8 pip)
 LoadModule wsgi_module /path/to/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so

 # Vhost
 <VirtualHost *:80>
    ServerName example1.example.com

    # The Python-home runs Python3.8 in venv
    WSGIDaemonProcess example1 processes=2 threads=15 display-name=%{GROUP} python-home=/home/ubuntu/web-apps/example1/venv
    WSGIProcessGroup example1
    WSGIScriptAlias / /home/ubuntu/web-apps/example1/web/app.wsgi

    <Directory /home/ubuntu/web-apps/example1/web>
        Options -Indexes +FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

# example1/web/app.wsgi
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
from web.app import application

# example1/web/app.py
# Will return "Hello. Python version is 3.8.0"
import sys
def application(environ, start_response):

    status = '200 OK'
    header = [('Content-type', 'text/html; charset=utf-8')]
    content = f"Hello. Python version is {sys.version}"

    start_response(status, header)
    content = content.encode('utf-8')
    return [content]
4

0 回答 0