Co-process: Also known as micro-threading, English name Coroutine.
Function: It has its own register context and stack, can keep the state of the last call, can pause the program at any time, switch back at any time.
Advantages:
Without the overhead of thread context switching
? No need for atomic operation locking and synchronization overhead
Easy to switch the control flow and simplify the programming model
? high concurrency + high scalability + Low cost: A CPU support for tens of thousands of processes is not a problem. So it's good for high concurrency processing.
Disadvantages:
? can not take advantage of multi-core resources: The nature of the process is a single-threaded, it can not simultaneously use multiple cores of a single CPU, the process needs and processes to run on multiple CPUs
A blocking (Blocking) operation, such as IO, blocks the entire program
def g (name): Print ("Starting product ...") while true:baozi= yield # program pauses waiting for next print ("{} is eating {} ...". Format (Nam E,baozi)) def p (): conn.__next__ () # instance conn start yield conn2.__next__ () # instance conn2 start yield i = 0 while i<5:i + 1 print ("p Roduct: {} ". Format (i)) conn.send (i) # send data to yield, yield recovery, and automatically perform next conn2.send (i) # send data to yield, yield recovery, and automatic execution nextconn = G ("Laoda") # Create Instance connconn2 = G ("Laoer") # Create Instance conn2product = P () # Start function P
There are actually two parameters for creating a co-object (Greenlet (Run=none, Parent=none)). The parameter "Run" is the method to be called, such as the function test1 () and Test2 () in the previous example, and the parameter "parent" defines the parent of the process object, that is to say, there can be a parent-child relationship between the Greenlet threads. If not or set to NULL, then its parent is the program's default "main" main coprocessor.
From Greenlet import Greenletdef test1 (): Print (1) t2.switch () # function paused, switch to T2 print (2) def test2 (): Print (3) T1.switch () # function paused, switch to T1 print (4) T1=greenlet (test1) # test1 Generate Greenlet Object T2=greenlet (test2) # Test2 Build greenl ET object T1.switch ()
Enables asynchronous I/O, operation
Method
|
Parameters
|
Role
|
Example
|
Spawn (Func,func_args)
|
Func: Function name added to Gevent Func_args: Function parameters
|
Set the function to create a co-process instance
|
|
Joinall[spawn_list]
|
Spawn_list:spawn Method List
|
Add the created coprocessor instance to the asynchronous list Wait for all instances in the list to finish executing |
|
Sleep (Time)
|
Time: (seconds)
|
Hand over the CPU control, time is seconds
|
|
getcurrent () |
|
get current coprocessor memory address |
|
From gevent import geventimport randomdef creat (num): wait_time = Random. Randomint (0,num) gevent.sleep (wait_time) print ("{} wait done!"). Format (Gevent.getcrrent ())) gevent_list = []for i in range: gevent_ List.append (Gevent.spawn (creat, i)) Gevent.joinall (gevent_list) #Socket并发: (not tested) Import geventimport socketclass server (object): def __int__ (Self,ip,port,*args): self.server_in = socket.socket () self.server_in.bind (Ip,port) self.server_in.listen (100) # self.spawn_list = [] def run (self): client_spawn = [] while true: conn,addr = self.server_in.accept () client_spawn.append (Gevent.spawn (Handler,conn)) gevent.joinall (Client_spawn) def handler (Self,conn):&NBSP;&NBSP;WHILE&NBSP;TRUE:&NBSP;&NBSP;&NBSP;RECV_DATA&NBSP;=&NBSP;CONN.RECV (1024x768) if recv_data = ' exit ': conn.shutdown (socket. SHUT_WR) break print ("recv:". Format (recv_data)) Conn.send (Recv_data.upper ())
http://python.jobbole.com/86481/
A detailed description of the process
"Python module": Co-Greenlet, Gevent