Recently i've devoted myself to deploying my app in my server. To briefly introduce my website, it is used for education with the assistance of ai conversation. Therefore, it needs more multi-user concurrency. :(
1、Former status and settings
The setting of my server is 2 cores 2 Gita-Byte, never cleaned up the cache or buffer. And as for the gunicorn setting
gunicorn -b 0.0.0.0:{port} main:app
It's so basic. So there is no surprise that i met some trouble.
2、Logs
When it comes to a user test, something wrong occured.
[date] [ERROR] Worker Timeout
Worker (pid:1927264) was sent SIGKILL! Perhaps out of memory?
The log was like that. At that time there was only 2 users visiting the website. I searched for help and the result was that the kernel killed the process because of lack of system storage.
Use this command to see your system storage
free -h
watch free -h
when use watch, you can see the dynamic storage.
Moreover, to type texts in the input box takes a lot of time. After typing, when the user tried to send their message, something wrong happened again.
3、Solutions
To deal with the issue, i came up with 3 solutions
- 1、upgrade my server to 2 cores 4 GB
- 2、clean up my buffer and cache
sync
echo 1 > /proc/sys/vm/drop_caches
- 3、Gunicorn, use the gevent
What is gevent?
Gevent is a coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libev event loop. By employing the gevent worker class, gunicorn can achieve concurrency when handling I/O-intensive tasks, making it suitable for managing a large number of concurrent connections and persistent connections. With this approach, even if a particular connection is awaiting I/O operations (such as database actions or network requests), other connections can be processed immediately, thus improving server performance.
To activate gevent
conda install gevent
gunicorn -k gevent -b 0.0.0.0:{port} main:app
- 4、To cope with the timeout question, use timeout when gunicorn-ing
gunicorn -w {2*core_number+1}-k gevent -b 0.0.0.0:{port} --timeout 600
So far, bugs have not caught. Good Luck for me.
Kill all the gunicorn processes
I deployed 2 projects in my server. Both of them use gunicorn and reflect to different ports. When I need to kill one of them, how am I supposed to do. I tried to kill them by searching the pid of port But I found that it won't work unless i kill the parent pid.
use this command to see all the process tree of gunicorn
pstree -ap | grep gunicorn
the result find the parent id of all your port.
and kill them by
kill -9 {pid}
in this case , the pid of port 5000 is 1929688 wait for a sec and try to see the pstree again.