Skip to content

Instantly share code, notes, and snippets.

@lwzm
Last active August 5, 2022 14:37
Show Gist options
  • Save lwzm/c7200fd8963f00ad3f3a6ff8058e98a9 to your computer and use it in GitHub Desktop.
Save lwzm/c7200fd8963f00ad3f3a6ff8058e98a9 to your computer and use it in GitHub Desktop.
gunicorn and pyinstaller and file-upload-app
#!/usr/bin/env python3
import shutil
from flask import Flask, Response, request
app = Flask(__name__)
@app.route('/<path:path>', methods=['GET', 'POST'])
def hello(path):
if request.method == "POST":
shutil.copyfileobj(request.stream, open(path, 'wb'), 4096)
return '', 201
else:
return Response(open(path, 'rb'), mimetype="application/octet-stream")
application = app
pyinstaller -y \
--hidden-import gunicorn.glogging \
--hidden-import gunicorn.workers.sync \
--hidden-import app \
file-server-app.py
upstream f {
server 127.0.0.4:3000;
keepalive 16;
}
server {
server_name f.*;
listen 443 ssl http2;
root /home/files;
location / {
proxy_buffering off;
proxy_request_buffering off;
if ($request_method = POST) {
proxy_pass http://f;
}
}
}
#!/usr/bin/env python3
from gunicorn.app.base import BaseApplication
class Application(BaseApplication):
def load_config(self):
s = self.cfg.set
s('bind', "0.0.0.0:8000")
s('workers', 3)
s('keepalive', 60)
s('timeout', 600)
s('accesslog', "-")
s('access_log_format', '%(t)s %(h)s "%(r)s" %(s)s %(b)s %(D)s "%(a)s"')
def load(self):
from app import application
return application
if __name__ == '__main__':
Application().run()
@YaoBeiji
Copy link

i pip install gunicorn==20.1.0
I setting s('threads',3) but...
Error: class uri 'gunicorn.workers.gthread.ThreadWorker' invalid or not found:

[Traceback (most recent call last):
File "gunicorn/util.py", line 139, in load_class
File "importlib/init.py", line 127, in import_module
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'gunicorn.workers.gthread'
]

@parsa-asgari
Copy link

parsa-asgari commented Apr 8, 2022

I added these two imports to the beginning of the server.py:

import gunicorn.glogging
import gunicorn.workers.sync

Then, renamed my flask file to __init__.py and placed it into a folder named app. So, now I have a server.py and a folder named app, which contains a __init__.py that hosts my web server logic.

#!/usr/bin/env python3

from gunicorn.app.base import BaseApplication
import gunicorn.glogging
import gunicorn.workers.sync
from app import app

class Application(BaseApplication):
    def load_config(self):
        s = self.cfg.set
        s('bind', "127.0.0.1:25400")
        s('workers', 3)
        s('keepalive', 60)
        s('timeout', 600)
        s('accesslog', "-")
        s('access_log_format', '%(t)s %(h)s "%(r)s" %(s)s %(b)s %(D)s "%(a)s"')

    def load(self):
        #from shahghoul_api_allinone import app
        return app

if __name__ == '__main__':
    Application().run()

It worked then.
Hope it helps you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment