1 gevent co-process
Co-process, also known as micro-threading, fiber. English name Coroutine. One sentence explains what a thread is: The process is a lightweight thread that is user-state .
The co-process has its own register context and stack. When the schedule is switched, the register context and stack are saved elsewhere, and the previously saved register context and stack are restored when it is cut back. So:
The process can retain the state of the last invocation (that is, a specific combination of all local states), each time the procedure is re-entered, which is equivalent to the state of the last call, in other words: The position of the logical stream at the last departure.
Benefits of the co-process:
- No overhead for thread context switching
- No need for atomic operation locking and synchronization overhead
- Easy switching of control flow and simplified programming model
- High concurrency + high scalability + Low cost: A CPU support for tens of thousands of processes is not a problem. Therefore, it is suitable for high concurrency processing.
Disadvantages:
- Unable to take advantage of multicore resources: The nature of the process is a single thread, it can not be a single CPU at the same time multiple cores, the process needs and processes to run on multi-CPU. Of course, most of the applications that we write in the day-out are not necessary, except for CPU-intensive applications.
- Blocking (Blocking) operations (such as IO) can block the entire program
Gevent
Gevent is a third-party library that makes it easy to implement concurrent or asynchronous programming through Gevent, and the main pattern used in Gevent is Greenlet, which is a lightweight coprocessor that accesses Python in the form of a C extension module. Greenlet all run inside the main program operating system process, but they are dispatched in a collaborative manner.
__author__='Administrator'Importgeventdeffoo ():Print('Running in Foo') Gevent.sleep (1) Print('Explicit context Switch to Foo again')defBar ():Print('Explicit context to bar') Gevent.sleep (1) Print('implicit context switch back to bar')defex ():Print('Explicit context to ex') Gevent.sleep (1) Print('implicit context switch back to ex') Gevent.joinall ([Gevent.spawn (foo), Gevent.spawn (bar), Gevent.spawn (ex),])
Gets the value
Running in Foo
Explicit Context to Bar
Explicit context to EX
Explicit context Switch to Foo again
Implicit context switch back to ex
Implicit context switch back to bar
Automatically switch tasks when IO blocking is encountered
fromGeventImportmonkey; Monkey.patch_all ()Importgevent fromUrllib.requestImportUrlopendeff (URL):Print('GET:%s'%URL) Resp=urlopen (URL) data=Resp.read ()Print('%d bytes received from%s.'%(len (data), URL)) Gevent.joinall ([Gevent.spawn (F,'https://www.python.org/'), Gevent.spawn (F,'https://www.yahoo.com/'), Gevent.spawn (F,'https://github.com/'),])
Gets the value
get:https://www.python.org/
get:https://www.yahoo.com/
get:https://github.com/
47381 bytes received from https://www.python.org/.
440972 bytes received from https://www.yahoo.com/.
25513 bytes received from https://github.com/.
Process finished with exit code 0
It is inefficient to implement multi-socket concurrency in single-threaded gevent by using the socketserver.
ImportSockethost='localhost' #The remote hostPORT = 8001#The same port as used by the servers =Socket.socket (socket.af_inet, socket. Sock_stream) S.connect ((HOST, PORT)) whiletrue:msg= Bytes (Input (">>:"), encoding="UTF8") S.sendall (msg) Data= S.RECV (1024) #print (data) Print('Received', repr (data)) S.close ()
Server
ImportSYSImportSocketImport TimeImportgevent fromGeventImportSocket,monkeymonkey.patch_all ()defServer (port): s=socket.socket () S.bind ('0.0.0.0', Port)) S.listen (500) whiletrue:cli, addr=s.accept () gevent.spawn (handle_request, CLI)defHandle_request (s):Try: whileTrue:data= S.RECV (1024) Print("recv:", data) s.send (data)if notData:s.shutdown (socket. SHUT_WR)exceptException as ex:Print(ex)finally: S.close ()if __name__=='__main__': Server (8001)
Client
Python coprocessor/Asynchronous Io/select\poll\epoll asynchronous IO and event driven