Gevent module with coroutine greenlet

'' ' 
Is defined: coroutine lightweight thread is a user-state 
Benefits: switching control flow, a simplified programming model 
high concurrency + + scalability cost: a million for the CPU coroutine support is not a problem. So it is suitable for highly concurrent processing 
without having atomic operations lock and synchronization overhead 
without having to thread context switching overhead 

Disadvantages: 
can not take advantage of multi-core resource: the nature of the coroutine is a single-threaded 
Coroutine fit the needs and processes to run on multiple CPU 
be blocked (blocking) operations (e.g., when the IO) will block off the entire program 


# greenlet implemented in C is a coroutine module, which allows you to easily switch between any of the function 
'' ' 
from greenlet Import greenlet 

DEF test1 ():
     Print ( " 123 " ) 
    g2.switch () 
    Print ( " III " ) 
    g1.switch () 

DEF test2 ():
     Print ( " 222" ) 
    G1.switch () 

g1 = greenlet (test1) 
g2 = greenlet (test2) 
g1.switch () 


# GEvent is a third-party library, you can easily implement synchronous or asynchronous concurrent programming by gevent, used mainly in gevent mode Greenlet 

Import GEVENT
 Import Time 

Start = the time.time ()
 DEF fun1 ():
     Print ( " AAAAAAA " ) 
    gevent.sleep ( 2 )
     Print ( " cccccccc " ) 

DEF fun2 ():
     Print ( " BBBBBBB " )
    gevent.sleep(1)
    print("dddddddd")

gevent.joinall([
    gevent.spawn(fun1),
    gevent.spawn(fun2)
])

end = time.time()
print(start- end)

 

Guess you like

Origin www.cnblogs.com/TKOPython/p/12486425.html