Installation (with CentOS as an example)
Gevent relies on libevent and Greenlet:
1. Installing Libevent
Direct Yum Install Libevent
Then configure the Python installation
2. Installing Easy_install
(1)
Wget-q http://peak.telecommunity.com/dist/ez_setup.py
(2) Use
Python ez_setup.py
(3) Use Easy_install to see if the command is available, and if it is not available, add the path
3. Installing Greenlet
(1)
Yum Install Python-devel
(2)
Easy_install Greenlet
4. Installing Gevent
Pip Install cython-e git://github.com/surfly/gevent.git@1.0rc2#egg=gevent
Tips for use
Gevent Library performance is high, but all along I have struggled with the Python Gil model resulting in threads that cannot preempt multi-core resources.
This multi-core mode that starts multiple Python processes requires additional front-end load balancing, such as LVS, which is a bit of a hassle.
The multiprocessing module and the Os.fork also allow two processes to duplicate the Accept event at the core of the event, causing the file handle to repeat the exception.
As for a process monitoring, multiple process processing mode, the monitoring of the process resource is not well allocated-is it independent allocation of a core or not separate allocation? If allocated separately, when the connection volume is small wasted a core, if not allocated, when the connection is large, the CPU will frequently switch processes.
It was only yesterday that Gevent was able to easily distribute its network model to multiple processes in parallel.
The secret is Gevent.fork ().
It used to be assumed that Gevent.fork was just a wrapper for greenlet.spawn. Gevent.fork can replace os.fork, not only to start a new process, but also to communicate their underlying event processing, parallel processing.
Import geventfrom gevent.server import Streamserverdef eat_cpu (): for I in Xrange (10000): Passdef CB (socket, address) : eat_cpu () socket.recv (1024x768) socket.sendall (' http/1.1, Ok\n\nhello world!! ') Socket.close () Server = Streamserver ((","), CB, backlog=100000) Server.pre_start () gevent.fork () Server.start_ Accepting () server._stopped_event.wait ()
After playing Monkey.patch_os, Os.fork can be replaced by gevent.fork, so that the multiprocessing module can also be used as usual, and achieve the effect of parallel processing.
from gevent import Monkey; Monkey.patch_os () from gevent.server import streamserverfrom multiprocessing import Processdef eat_cpu (): For I in Xrange (10000): Passdef CB (Socket, address): eat_cpu () socket.recv (1024x768) socket.sendall (' http/1.1 OK \n\nhello world!! ') Socket.close () Server = Streamserver ((","), CB, backlog=100000) Server.pre_start () def serve_forever (): Server.start_accepting () server._stopped_event.wait () Process_count = 4for i in range (process_count-1): Process (Target=serve_forever, Args=tuple ()). Start () Serve_forever ()