简体   繁体   English

使用 nginx 运行 Pyinstaller Django 应用程序

[英]Running a Pyinstaller Django application using nginx

I have packaged a django application using pyinstaller我已经使用 pyinstaller 打包了一个 django 应用程序

python pyinstaller.py --name=executablename Tool/manage.py

The problem is that the default server is single threaded and cannot handle much load.问题是默认服务器是单线程的,无法处理太多负载。

Therefore i want to use a standard server with my packaged django executable.因此,我想将标准服务器与我打包的 django 可执行文件一起使用。 I have two choices eighther to use我有两个选择可以使用

1. Apache
2. Nginx with uwsgi

I can easily setup them with code, but the issue is related to packaged application.我可以很容易地用代码设置它们,但问题与打包的应用程序有关。 Here both server want a wsgi file which is normally present in Tool\wsgi.py but since this is a packaged application so not wsgi.py is present therefore both server cannot be attached.这里两个服务器都需要一个通常存在于 Tool\wsgi.py 中的 wsgi 文件,但由于这是一个打包的应用程序,因此不存在 wsgi.py,因此无法连接两个服务器。

Does anyone one know a way to achieve this.有谁知道实现这一目标的方法。 I know advised way would be to go with source but i really do not want to distribute my source in python files.我知道建议的方法是将源代码发送到 go,但我真的不想在 python 文件中分发我的源代码。

uwsgi can't run not python file,so you can use another python file to start uwsgi service. uwsgi不能运行不是python文件,所以你可以使用另一个python文件来启动uwsgi服务。

start.py开始.py

import os 


init_file = "./uwsgi.ini"

result = os.system("uwsgi --ini {}".format(init_file))

and then use pyinstaller to run start.spec然后使用 pyinstaller 运行 start.spec

# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_submodules, collect_all


block_cipher = None

hiddenimports = collect_submodules('rest_framework')

hiddenimports.extend(
    [
        'django.contrib.contenttypes',
        'django.contrib.staticfiles',
        'django_filters',
        'rest_framework',
        'api',
        'coreschema',
    ]
)

a = Analysis(['start_service.py'],
             pathex=['/src'],
             binaries=[],
             datas=[
               (r'/env/lib/python3.6/site-packages/rest_framework/', './rest_framework'),
               (r'/env/lib/python3.6/site-packages/django_filters/', './django_filters')
             ],
             hiddenimports=hiddenimports,
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='manage',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='start')

run code运行代码

pyinstaller start.spec

it will generate file in /dist/start/start它将在 /dist/start/start 中生成文件

and run./dist/start/start, it will start uwsgi service然后运行./dist/start/start,它会启动uwsgi服务

If using uWSGI, there is a command line option which allows you to say you are providing it with a module path instead of a file path. 如果使用uWSGI,则有一个命令行选项,允许您说它为其提供模块路径而不是文件路径。 For standard Apache with mod_wsgi, or with uWSGI defaults, you can create a WSGI script file which imports the WSGI application from your module by its path into the WSGI script file and then refer to that WSGI script file. 对于带有mod_wsgi或uWSGI缺省值的标准Apache,您可以创建一个WSGI脚本文件,该文件通过其路径将WSGI应用程序从模块导入WSGI脚本文件,然后引用该WSGI脚本文件。 If using mod_wsgi-express, then it has an option like uWSGI which allows you to say you are providing it with a module path instead. 如果使用mod_wsgi-express,那么它有一个像uWSGI这样的选项,它允许你说你正在为它提供一个模块路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM