利用 gevent 给网站加个速
进入 /usr/lib/systemd/system
文件夹
(base) ➜ system cat xxx.service [Unit] After=syslog.target network.target remote-fs.target nss-lookup.target [Service] # 你的用户 User=root # 你的目录 WorkingDirectory=/temp/website/www.xxx.com/ # gunicorn启动命令 ExecStart=/root/.local/share/virtualenvs/www.xxx.com-7iK-eqgK/bin/gunicorn --worker-class=gevent --bind unix:/tmp/www.heigejia.com.socket cms_v1.wsgi:application Restart=on-failure [Install] WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
(
base
)
➜
system
cat
xxx
.
service
[
Unit
]
After
=
syslog
.
target
network
.
target
remote
-
fs
.
target
nss
-
lookup
.
target
[
Service
]
# 你的用户
User
=
root
# 你的目录
WorkingDirectory
=
/
temp
/
website
/
www
.
xxx
.
com
/
# gunicorn启动命令
ExecStart
=
/
root
/
.
local
/
share
/
virtualenvs
/
www
.
xxx
.
com
-
7iK
-
eqgK
/
bin
/
gunicorn
--
worker
-
class
=
gevent
--
bind
unix
:
/
tmp
/
www
.
heigejia
.
com
.
socket
cms_v1
.
wsgi
:
application
Restart
=
on
-
failure
[
Install
]
WantedBy
=
multi
-
user
.
target
|
–worker-class
指定工作方式,这里我们用gevent
gunicorn --worker-class=gevent isaced.wsgi:application gunicorn wsgi:application #8个worker gunicorn -w 8 wsgi:application #指定端口号 gunicorn -w 8 -b 0.0.0.0:8888 wsgi:application #unix socket gunicorn -w 8 --bind unix:/xx/mysock.sock wsgi:application #使用gevent做异步(默认worker是同步的) gunicorn -w 8 --bind 0.0.0.0:8000 -k 'gevent' wsgi:application #选项挺多,看文档或者使用 --help都可以查看 --log-level=DEBUG 日志级别 --timeout=100 超时时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
gunicorn
--
worker
-
class
=
gevent
isaced
.
wsgi
:
application
gunicorn
wsgi
:
application
#8个worker
gunicorn
-
w
8
wsgi
:
application
#指定端口号
gunicorn
-
w
8
-
b
0.0.0.0
:
8888
wsgi
:
application
#unix socket
gunicorn
-
w
8
--
bind
unix
:
/
xx
/
mysock
.
sock
wsgi
:
application
#使用gevent做异步(默认worker是同步的)
gunicorn
-
w
8
--
bind
0.0.0.0
:
8000
-
k
'gevent'
wsgi
:
application
#选项挺多,看文档或者使用 --help都可以查看
--
log
-
level
=
DEBUG
日志级别
--
timeout
=
100
超时时间
|
如何查看当前 linux 是几核
给你的 django 网站加入 缓存技术
修改 settings.py
加入缓存配置
CACHES = { 'default': { 'BACKEND': '<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/django" title="View all posts in django" target="_blank">django</a></span>.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/temp/website/www.xxx.com/tmp/<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/django" title="View all posts in django" target="_blank">django</a></span>_cache', 'TIMEOUT': 600, 'OPTIONS': { 'MAX_ENTRIES': 1000 } } }
1
2
3
4
5
6
7
8
9
10
11
|
CACHES
=
{
'default'
:
{
'BACKEND'
:
'django.core.cache.backends.filebased.FileBasedCache'
,
'LOCATION'
:
'/temp/website/www.xxx.com/tmp/django_cache'
,
'TIMEOUT'
:
600
,
'OPTIONS'
:
{
'MAX_ENTRIES'
:
1000
}
}
}
|
修改 views.py
from django.views.decorators.cache import cache_page @cache_page(60 * 15) # 秒数,这里指缓存 15 分钟,不直接写900是为了提高可读性 def video_index(request): ... return xxx
1
2
3
4
5
6
7
8
|
from
django
.
views
.
decorators
.
cache
import
cache
_page
@
cache_page
(
60
*
15
)
# 秒数,这里指缓存 15 分钟,不直接写900是为了提高可读性
def
video_index
(
request
)
:
.
.
.
return
xxx
|