Python full stack development day32-process creation, process synchronization, inter-process communication, process pooling

Source: Internet
Author: User
Tags semaphore ticket

I. Summary of CONTENTS 1. Process creation 1) process: Two ways to create a new process:

1. Instantiate the process, pass the parameter through the args= (,) tuple, 2 Create the class inherits the process, the class initializes the argument

2) P.join (), block the main process, after executing p process, release

3) daemon, daemon code execution complete, P.daemon = True

Import Time fromMultiprocessingImportProcessdeffunc (): whileTrue:Print('is alive') Time.sleep (0.5)defWahaha (): I=0 whileI < 5:        Print('First%s seconds'%i) Time.sleep (1) I+ = 1if __name__=='__main__': P2= Process (target=Wahaha) P2.start () P1= Process (target=func) P1.daemon= True#set P1 as daemonP1.start () time.sleep (3)    Print('Main Process') P2.join ()#The daemon waits for the child process to end before it ends the daemon
Daemon Code

4) P.terminate () will not end immediately, wait for the main process to reclaim resources, etc.

# python and process
# multiprocessing Module
# main Process
# Parent-Child process
# The parent process needs to wait until the child process finishes executing
# Some resources that are responsible for recycling sub-processes
# data isolation between processes and processes
# child processes can receive parameters at the beginning of creation
# but you can't return a value directly
# The work between asynchronous parent-child processes is non-disruptive
# Open a sub-process
# multiprocessing. Process
# The first way
# instantiation Creates a process
# start opens a process
# The second way
# Create a process with object-oriented form
# Inherit Process
# Pass parameters at Init
# Rewrite the Run method
# Control of child processes
# Join

2. Synchronization of processes

1), Locking lock

Lock, maintain the security of the data, but reduce the efficiency of the program, but all the efficiency must be based on data security;

When it comes to concurrent programming, we have to consider the security of shared data, and I need to be cautious about data manipulation in the Concurrency section,

Lock control is necessary if data insecurity is involved.

#example of a ticketImportOs,time fromMultiprocessingImportlock,processImportJSONdefSearch (i): With open ('DB') as F:ticket_count=json.load (f)Print('%s found the remaining:%s Liao Ticket! '% (i,ticket_count['Count']))defBuy (i): With open ('DB') as F:ticket_count=json.load (f) time.sleep (0.2)#Data Network transmission delay of simulated request database    ifticket_count['Count'] >0:ticket_count['Count']-= 1Print('the ticket was bought at the%s number! '%i) Time.sleep (0.2)## Analog Write database data network transmission delayWith open ('DB','W') as F:json.dump (Ticket_count, F)defGet_ticket (lock, I): Search (i) with lock:buy (i )#two kinds of locking method##def get_ticket (lock, i):#Search (i) # Tickets are not locked, can be checked#Lock.acquire ()#Buy (i) # buy tickets and lock#lock.release ()if __name__=='__main__': Lock=Lock () forIinchRange (10): P= Process (target=get_ticket,args=(Lock,i)) P.start ()
example of a ticket check

Lock internally implements inter-process communication, which makes it clear who acquire the lock, and who realease it in several child processes that have the lock parameter.

2), Semaphore Semaphore Lock + counter

      

#example of KTV for 4 people#def KTV (sema,i):#Sema.acquire ()#print ('%s ' into ktv! '% i)#Time.sleep (Random.randint (1,5))#print ('%s out of ktv!!! '% i)#sema.release ()defKTV (sema,i): with SEMA:Print('%s into ktv! '%i) Time.sleep (Random.randint (1,5))        Print('%s out of ktv!!! '%i)if __name__=='__main__': Sema= Semaphore (4)     forIinchRange (10): P= Process (Target=ktv, args=(Sema, i)) P.start ()
example of KTV for 4 people

3) Events Event

A mechanism that controls whether a child process is blocked or executed:

Wait method:

If the Is_set () flag bit is false, blocking wait (n) can be set to block n seconds after execution, but the flag bit does not change or false

If the Is_set () flag bit is true, the effect is equivalent to pass

Control Flags:

Is_set (): Determine flag bit status

Set (): Sets the flag bit to True

Clear (): Set flag bit false

ImportTime , Random fromMultiprocessingImportProcess,evente=Event ()#print (E.is_set ()) will be printed 12 times, count it hahadefTraffic_light (E):Print('\033[1;31m The red light \033[0m')     whileTrue:time.sleep (2)        ifE.is_set ():Print('\033[1;31m The red light \033[0m') e.clear ()Else:            Print('\033[1;32m The green light \033[0m') E.set ()defcar (E, i):if  notE.is_set ():Print('car%s waiting for a red light'%i) e.wait ()Print('car%s through'%i)if __name__=='__main__': E=Event () traffic_light1= Process (Target=traffic_light, args=(E,)) Traffic_light1.daemon=True traffic_light1.start () Li= []     forIinchRange (10): P= Process (Target=car, args=(E, i)) P.start () Li.append (p) time.sleep (random.randint (0,3))     forPinchLi:p.join ()Print('The main program is over! ')
example of event traffic lights

3. Inter-process communication 1. Queues queue

# Communication between IPC processes

Empty (): # Multiple process cases are not allowed:

Full (): # Multiple process cases are not allowed:

Put (): #队列慢 will cause the program to block:

Get (): #队列空 can cause blocking of the program:

Put_nowait (): # No blocking, but easy data loss

Get_nowait (): # will not block, but full will be an error

      

 fromMultiprocessingImportProcess,queuedefnum (q,i): Q.put ({i:i**i})if __name__=='__main__': Q=Queue () forIinchRange (10): P= Process (Target=num, args=(Q,i)) P.start () forJinchRange (10):        Print(Q.get ())
example of process communication using queue

2. Pipe 3.Manager 4. Process Pool

Pool

  

Second, preview and expand

Python full stack development day32-process creation, process synchronization, inter-process communication, process pooling

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.