One gevent module
Gevent is a third-party library that makes it easy to implement concurrent or asynchronous programming through Gevent. The main mode used in Gevent is Greenlet, which is a lightweight process 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.
# usage G1=gevent.spawn (func,1,2,3,x=4,y=5) creates a co-object g1,spawn the first argument in parentheses is the function name, such as Eat, which can be followed by multiple arguments, either positional arguments or keyword arguments, which are passed to the eat of the function g2 = Gevent.spawn (FUNC2) G1.join () # Wait for G1 to end G2.join () # Wait for the G2 to end # or the two-step cooperation step: Gevent.joinall ([g1,g2]) G1.value # Get the return value of func1.
Automatically switch tasks when IO blocking is encountered
Import Geventdef Eat (name): print ('%s eat 1 '% name) Gevent.sleep (2) print ('%s eat 2 '% name) def play (name): C3/>print ('%s play 1 '% name) gevent.sleep (1) print ('%s play 2 '% name) G1 = Gevent.spawn (Eat, ' mike ') g2 = Gevent.s Pawn (play, ' Mike ') G1.join () G2.join () print (' master ')
The above example Gevent.sleep (2) simulates an IO block that gevent can identify
and Time.sleep (2) or other blocking, gevent is not directly recognized the need to use the following line of code, patching, you can identify the
From gevent import Monkey;monkey.patch_all () must be placed in front of the patched person, such as the Time,socket module
Or we simply remember: To use gevent, you need to put the from Gevent import Monkey;monkey.patch_all () to the beginning of the file
From gevent import monkey;monkey.patch_all () Import Geventimport timedef Eat (name): print ('%s eat 1 '% name) time . Sleep (2) print ('%s eat 2 '% name) def play (name): print ('%s play 1 '% name) time.sleep (1) print ('%s play 2 '% name) G1 = Gevent.spawn (Eat, ' mike ') g2 = Gevent.spawn (play, ' Mike ') G1.join () G2.join () print (' main ')
We can use Threading.current_thread (). GetName () to view each G1 and G2, viewing the result as dummythread-n, which is a dummy thread
Two exercises
# Server from gevent import Monkey;monkey.patch_all () from socket import *import gevent# If you do not want to patch with Monkey.patch_all (), Can be used with gevent socket# from gevent import socket# s = Socket.socket () def server (server_ip, port): s = socket (af_inet, SOC K_stream) s.setsockopt (Sol_socket, SO_REUSEADDR, 1) S.bind ((server_ip, Port)) S.listen (5) while true: conn, addr = s.accept () gevent.spawn (talk, Conn, addr) def talk (conn, addr): try: While True: res = CONN.RECV ( ' client%s:%s msg:%s '% (Addr[0], addr[1], res)) Conn.send (Res.upper ()) except Exception as E: print (E) finally: conn.close () if __name__ = = ' __main__ ': server (' 127.0.0.1 ', 8080)
Client
From threading import threadfrom socket import *import threadingdef client (server_ip, port): c = socket (Af_inet, Sock_ STREAM) # The Socket object must be added to the function, that is, within the local namespace, in the outside of the function is shared by all threads, then everyone common one socket object, then the client port is always the same C.connect ((server_ip, Port)) count = 0 while True: c.send (('%s say hello%s '% (Threading.current_thread (). GetName (), count)). Encode (' Utf-8 ')) msg = C.RECV (1024x768) print (Msg.decode (' Utf-8 ')) count + = 1if __name__ = = ' __main__ ': for i in Range: t = Thread (target=client, args= (' 127.0.0.1 ', 8080)) T.start ()
Python concurrent programming: Co-process-gevent module