The significance of Gevent library lies in the concurrent high-performance network programming support. here we will explain how to install and use Gevent in the Python network programming library. let's take a look at the multi-process programming supported by Gevent:
Installation (take CentOS as an example)
Gevent depends on libevent and greenlet:
1. install libevent
Direct yum install libevent
Then configure python installation
2. install easy_install
(1)
wget -q http://peak.telecommunity.com/dist/ez_setup.py
(2) use
python ez_setup.py
(3) use easy_install to check whether the command is available. if not, add the PATH to the PATH.
3. install greenlet
(1)
yum install python-devel
(2)
easy_install greenlet
4. install gevent
pip install cython -e git://github.com/surfly/gevent.git@1.0rc2#egg=gevent
Tips
The Gevent Library has a high performance, but I have been entangled in the GIL model of python, so that threads cannot seize multi-core resources.
The use of multi-core mode to start multiple python processes requires adding front-end load balancing, such as lvs, which is somewhat troublesome.
The multiprocessing module and OS. fork make the two processes register the accept event repeatedly at the event core, resulting in duplicate file handles.
As for a single process listener, in the mode of processing multiple processes, it is difficult to allocate the resources of the monitored process-whether to allocate a core independently or not separately? If it is allocated separately, a core is wasted when the connection volume is small. if it is not allocated, the cpu will switch the process frequently when the connection volume is large.
Yesterday, we found that gevent can easily distribute its network model to multiple processes for parallel processing.
The secret lies in gevent. fork ().
In the past, it was assumed that gevent. fork was just a package of greenlet. spawn. Gevent. fork can replace OS. fork and not only start a new process, but also communicate the underlying event processing for parallel processing.
import geventfrom gevent.server import StreamServerdef eat_cpu(): for i in xrange(10000): passdef cb(socket, address): eat_cpu() socket.recv(1024) socket.sendall('HTTP/1.1 200 OK\n\nHello World!!') socket.close()server = StreamServer(('',80), cb, backlog=100000)server.pre_start()gevent.fork()server.start_accepting()server._stopped_event.wait()
With monkey. patch_ OS, OS. fork can be replaced by gevent. fork, so that the multiprocessing module can be used as usual and achieve 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(1024) socket.sendall('HTTP/1.1 200 OK\n\nHello World!!') socket.close()server = StreamServer(('',80), 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()