Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Pyinstaller Django application using nginx

I have packaged a django application using pyinstaller

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. 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.

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.

like image 473
duck Avatar asked Nov 27 '22 06:11

duck


1 Answers

uwsgi can't run not python file,so you can use another python file to start uwsgi service.

start.py

import os 


init_file = "./uwsgi.ini"

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

and then use pyinstaller to run 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

and run ./dist/start/start, it will start uwsgi service

like image 87
Enzoooo_ Avatar answered Nov 28 '22 19:11

Enzoooo_