網站部署後,基本一天有時候幾個小時就會502,查看uwsgi日誌看到,一直在報MemoryError的錯python
以前覺得是python版本的問題,說是32位的python最多支持2G,但查看了python版本,是64位的啊web
>>> import platform
>>> platform.architecture()
('64bit', 'ELF')
>>>
因而懷疑程序問題,內存佔用到了物理內存的極限,因此加了2G虛擬內存服務器
[root@iZbp1cixaslir6lcn0qcdvZ ~]# free -m
total used free shared buffers cached
Mem: 16081 5502 10578 0 208 3702
-/+ buffers/cache: 1591 14489
Swap: 2047 0 2047
甚至發現,系統內存使用量還不到1/3app
最後查看了下uwsgi的配置文件的文檔socket
socket : 地址和端口號,例如:socket = 127.0.0.1:50000
processes : 開啓的進程數量
workers : 開啓的進程數量,等同於processes(官網的說法是spawn the specified number of workers / processes)
chdir : 指定運行目錄(chdir to specified directory before apps loading)
wsgi-file : 載入wsgi-file(load .wsgi file)
stats : 在指定的地址上,開啓狀態服務(enable the stats server on the specified address)
threads : 運行線程。因爲GIL的存在,我以爲這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 容許主進程存在(enable master process)
daemonize : 使進程在後臺運行,並將日誌打到指定的日誌文件或者udp服務器(daemonize uWSGI)。實際上最經常使用的,仍是把運行記錄輸出到一個本地文件上。
log-maxsize :以固定的文件大小(單位KB),切割日誌文件。 例如:log-maxsize = 50000000 就是50M一個日誌文件。
pidfile : 指定pid文件的位置,記錄主進程的pid號。
vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
disable-logging : 不記錄請求信息的日誌。只記錄錯誤以及uWSGI內部消息到日誌中。若是不開啓這項,那麼你的日誌中會大量出現這種記錄:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
log-maxsize: 日誌大小,當大於這個大小會進行切分 (Byte)
log-truncate: 當啓動時切分日誌
發現有這兩個參數post
limit-as 和 roload-on-as網站
原來limit-as是512,也就是每一個進程最大虛擬內存只有512,故試着把這個參數調大到2048,並加一個reload-on-as = 1024,在達到1024的時候重啓進程雙保險spa
因而修改配置文件以下線程
socket = 127.0.0.1:8888
master = true
vhost = true
no-site = true
workers = 2
reload-mercy = 10
vacuum = true
max-requests = 1000
limit-as = 2048
reload-on-as = 1024
buffer-size = 30000
pidfile = /var/run/uwsgi8888.pid
daemonize = /website/uwsgi8888.log
結果按照這個配置,果真再也不出現MemoryError的問題了
unix