Skip to content

Commit

Permalink
feat: 添加web服务器gunicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
shaohuzhang1 authored Jul 19, 2024
1 parent 4d8ac28 commit 1685159
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
45 changes: 45 additions & 0 deletions apps/common/management/commands/gunicorn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎
@file: gunicorn.py
@date:2024/7/19 17:43
@desc:
"""
import os
import subprocess

from django.core.management.base import BaseCommand

from smartdoc.const import BASE_DIR


class Command(BaseCommand):
help = 'My custom command'

# 参数设定
def add_arguments(self, parser):
parser.add_argument('-b', nargs='+', type=str, help="端口:0.0.0.0:8080") # 0.0.0.0:8080
parser.add_argument('-k', nargs='?', type=str,
help="workers处理器:uvicorn.workers.UvicornWorker") # uvicorn.workers.UvicornWorker
parser.add_argument('-w', action='append', type=str, help='worker 数量') # worker 数量
parser.add_argument('--max-requests', action='append', type=str, help="最大请求") # 10240
parser.add_argument('--max-requests-jitter', action='append', type=str)
parser.add_argument('--access-logformat', action='append', type=str) # %(h)s %(t)s %(L)ss "%(r)s" %(s)s %(b)s

def handle(self, *args, **options):
log_format = '%(h)s %(t)s %(L)ss "%(r)s" %(s)s %(b)s '
cmd = [
'gunicorn', 'smartdoc.asgi:application',
'-b', options.get('b') if options.get('b') is not None else '0.0.0.0:8080',
'-k', options.get('k') if options.get('k') is not None else 'uvicorn.workers.UvicornWorker',
'-w', options.get('w') if options.get('w') is not None else '5',
'--max-requests', options.get('max_requests') if options.get('max_requests') is not None else '10240',
'--max-requests-jitter',
options.get('max_requests_jitter') if options.get('max_requests_jitter') is not None else '2048',
'--access-logformat',
options.get('access_logformat') if options.get('access_logformat') is not None else log_format,
'--access-logfile', '-'
]
kwargs = {'cwd': BASE_DIR}
subprocess.run(cmd, **kwargs)
3 changes: 2 additions & 1 deletion apps/smartdoc/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
'rest_framework',
"drf_yasg", # swagger 接口
'django_filters', # 条件过滤
'django_apscheduler'
'django_apscheduler',
'common'

]

Expand Down
9 changes: 8 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def perform_db_migrate():


def start_services():
management.call_command('migrate')
management.call_command('gunicorn')


def runserver():
management.call_command('runserver', "0.0.0.0:8080")


Expand All @@ -70,6 +73,10 @@ def start_services():
perform_db_migrate()
elif action == "collect_static":
collect_static()
elif action == 'dev':
perform_db_migrate()
runserver()
else:
collect_static()
perform_db_migrate()
start_services()
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jieba = "^0.42.1"
diskcache = "^5.6.3"
pillow = "^10.2.0"
filetype = "^1.2.0"
torch = "^2.2.1"
torch = "2.2.1"
sentence-transformers = "^2.2.2"
blinker = "^1.6.3"
openai = "^1.13.3"
Expand All @@ -42,6 +42,9 @@ websocket-client = "^1.7.0"
langchain-google-genai = "^1.0.3"
openpyxl = "^3.1.2"
xlrd = "^2.0.1"
gunicorn = "21.2.0"
python-daemon = "3.0.1"
uvicorn = "0.22.0"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 1685159

Please sign in to comment.