Python's co-process

Source: Internet
Author: User

Multithreading concurrency, including the thread pool, is concurrency controlled by the operating system. If it is single-threaded, concurrency can be implemented through a single thread.

Also known as micro-threading, is a user-state of the lightweight thread, by the user program control scheduling.

The python thread is at the kernel level and is controlled by the operating system (such as a single thread experiencing IO or executing too long will be forced to hand over the CPU execution permissions and switch other threads to run)

and the single-line thread open the process, once encountered Io, by the user control scheduling.

Characteristics:

1. Single Thread concurrency

2. Modify shared data without shackles

3, the user program to save multiple control flow context stack

4, attach: A co-process encountered the IO operation automatically switch to other Yield,greenlet (can not implement the detection Io, with the Gevent module select mechanism)

Concurrent:

#Note that the consumer function is a generator (generator):#any function that contains the yield keyword will automatically become the Generator (Generator) objectdefconsumer (): R="'     whileTrue:#3, consumer through yield to get the message, processing, and through yield to the results passed back;        #The yield directive has the function of the return keyword. The stack of functions is then automatically frozen (freeze) in this line.         #The next time the function caller uses next () or generator.send () or for-in to call the function again,        #begins on the next line of the yield code, resumes execution, and returns the next iteration result. In this way, iterators can implement infinite sequences and lazy evaluation. n =yieldRif  notN:return        Print('[consumer]←←consuming%s ...'%N) time.sleep (1) R='OK'defProduce ():#1. First call C.next () to start the generatorc =consumer () next (c) n=0 whileN < 5: N= n + 1Print('[producer]→→producing%s ...'%N)#2. Then, once the thing is produced, switch to consumer execution by C.send (n);CR =c.send (n)#4, produce get consumer processing results, continue to produce the next message;        Print('[PRODUCER] Consumer return:%s'%CR)#5, produce decided not to produce, through C.close () Close the consumer, the whole process is over. c.close ()if __name__=='__main__': Start_time=time.time ()#6, the entire process is not locked, by a thread execution, produce and consumer collaboration to complete the task, so called "co-process", in a thread to work together to complete. res =Produce ()#Consumer (RES)End_time =time.time ()Print(End_time-start_time)

Serial:

defProduce (): N=0 Res= []     whileN < 5: N= n + 1Print('[producer]→→producing%s ...'%N) res.append (n)returnResdefConsumer (res):Passif __name__=='__main__': Start_time=time.time ()#6, the entire process is not locked, by a thread execution, produce and consumer collaboration to complete the task, so called "co-process", in a thread to work together to complete. res =Produce ()Consumer (RES)End_time =time.time ()Print(End_time-start_time)

Greenlet module for easy switching, but does not automatically switch to other tasks when IO is encountered

 fromGreenletImportGreenletdefEat (name):Print('%s is eating 1'%name) Gr2.switch ('Zhanwu')    Print('%s is eating 2'%name) Gr2.switch ()defPlay (name):Print('%s is playing 1'%name) Gr1.switch ()Print('%s is playing 2'%name) Gr1=Greenlet (Eat) GR2=Greenlet (play) Gr1.switch ('Egon') #切换的时候传参

Gevent module, asynchronous submit task, when encountering IO can realize automatic switch.

After the task is submitted asynchronously, the main thread ends, causing the child thread to finish the task, and the main thread through sleep or join does not die until the child threads run to the end.

 from Import Monkeymonkey.patch_all ()    # Turn the following blocking operation into a non-blocking operation, which is required with gevent
defEat (name):Print('%s is eating 1'%name) Gevent.sleep (3)    Print('%s is eating 2'%name)defPlay (name):Print('%s is playing 1'%name) Gevent.sleep (4)    Print('%s is playing 2'%name) Gevent.joinall ([Gevent.spawn (Eat,'Egon'), Gevent.spawn (play,'Egon')])

Enables concurrency on a single thread based on the gevent.

if more than one process is opened, multiple threads are opened in each process, and each thread is re-opened, which can greatly improve the efficiency. Server Side fromGeventImportMonkey,spawn;monkey.patch_all () fromSocketImport*defTalk (conn): whileTrue:Try: Data= CONN.RECV (1024)            if  notData BreakConn.send (Data.upper ())exceptConnectionreseterror: Breakconn.close ()defServer (Ip,port):#to a client, starting a connServer =socket (af_inet, Sock_stream) server.setsockopt (Sol_socket, SO_REUSEADDR,1) server.bind ((IP, Port)) Server.listen (5)     whiletrue:conn,addr=server.accept () spawn (Talk,conn) server.close ()if __name__=='__main__': G= Spawn (Server,'127.0.0.1', 8087) G.join () Client side: fromSocketImport* fromThreadingImportThread,currentthreaddefClient (): Client=socket (af_inet, Sock_stream) client.connect (('127.0.0.1', 8087))     whileTrue:client.send (('%s Say hello'%currentthread (). GetName ()). Encode ('UTF8')) Data= CLIENT.RECV (1024)        Print(Data.decode ('Utf-8'))if __name__=='__main__':     forIinchRange (500): T= Thread (target=client) T.start ()

Python's co-process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.