Python Multi-process

Source: Internet
Author: User

The first way to open a process
#!/usr/bin/python# -*- coding:utf-8 -*-from multiprocessing import processimport  time, random, os# print (Os.cpu_count ()) ### #  defines a task # def piao (name): #      print ('%s is piaoing '  % name) #      Time.sleep (3)   # cpu block, switch to main process #     print ('%s is piao  End '  % name) ### if __name__ ==  ' __main__ ':#     #  Target specifies the execution target, args specifies the positional parameter, is specified as a tuple, Kwargs specifies the dictionary #     p1 = process (Target=piao,  args= (' LH ',), name= ' <p1> ')   #  Generate object #     #  Create child process, Assign the main process address space, open a child process, the creation process takes time,#     #  send system call, just a system call, command operating system promoter #      # , the main process does not wait for the subprocess #     p1.start ()   #  to open the child process #      #&nbsP;time.sleep (1)   ##     print (' main process ') #     #  Who runs first, who prints first?
The second type of custom process
Class Piao (Process): Def __init__ (self, name): Super (). __init__ () # Reusing the parent class method, Self.name = name def run ( Self): # The method name must be run print ('%s is piaoing ... '% self.name) time.sleep (3) print ('%s is Piao end ' percent self . name) If __name__ = = ' __main__ ': p1 = Piao (' lh ') P1.start () # equals P1.run () print (' main process ')
Lock
#!/usr/bin/python# -*- coding:utf-8 -*-# import threading, time## v =  10## lock = threading. Lock ()   #只能有一个使用锁 # lock = threading. Rlock ()   #  recursive lock ### def task (ARG): #     time.sleep (2) #      lock.accquire ()   #  request to use lock, other thread waits #     global  v#     v -= 1#     print (v) #      lock.release ()   #  return release # for i in range (TEN):#      t = threading. Thread (target=task, args= (i)) #     t.start () #  Many people use locks at the same time, batch operations # import  threading, time## v = 10## lock = threading. Boundedsemaphore (3)   #同时三个使用该锁 # # lock = threading. Rlock ()   #  recursive lock ### def task (ARG): ##     lock.acquire ()   #  request to use lock, other thread waits #      time.sleep (1) #     global v#     v -= 1 #     print (v) #     lock.release ()   #  return release # #  for i in range (): #     t = threading. Thread (target=task, args= (i,)) #     t.start () #  time lock. Limitation of the release lock # import  threading, time## v = 10## lock = threading. Event ()   #  three use the lock ### # lock = threading. Rlock ()   #  recursive lock ### def task (ARG): #     time.sleep (1) #      lock.wait ()   #  lock Multi-thread #     print (ARG) ###  For i in range (): #     t = threading. ThreaD (target=task, args= (I,)) #     t.start () ## while true:#      value = input (' >>> ') #     if value ==   ' 1 ': #         lock.set () #          lock.clear () #想怎么锁就怎么锁import  threading, timev = 10lock =  Threading. Condition ()   #  Three simultaneous use of the lock # lock = threading. Rlock ()   #  recursive lock Def task (ARG):     time.sleep (1)      Lock.acquire ()     lock.wait () #  Locking multiple threads     print (ARG)      lock.release () For i in range ():     t = threading. Thread (target=task, args= (i,))     t.start () while true:     Value = input (' >>> ')   &nBsp; lock.acquire ()     lock.notify (int (value))     lock.release () 
Methods and properties of process objects
From multiprocessing import processimport time, random# def piao (name): #      print ('%s is piaoing '  % name) #      Time.sleep (Random.randint (1, 3)) #     print ('%s is piao end '  % name) ### if __name__ ==  ' __main__ ': #     p1 =  process (target=piao, args= (' Egon ',)) #     p2 = process (target= piao, args= (' LH ',)) #     p3 = process (target=piao, args= (' Apla ',) ) #     p4 = process (target=piao, args= (' Tom ',)) ##      #  Open order not necessarily, who first executes who prints #     p1.start ()   #  is not a blocking operation in the outgoing system call #      p2.start ()   #  is not a blocking operation in the outgoing system call #      P3.start ()   #  not blockedOperation, calling #     p4.start () in the outgoing system ()   #  not blocking operation, calling ##     in the outgoing system  # p1.join ()   #  main process waits for P1 to execute, hold#     # p1.join ()    #  main process Wait #     # p2.join () #     # p3.join () #     # p4.join () #     p_l = [p1, p2,  p3, p4]#     for p in p_l:#          p.start () ##     for p in p_l:#          p.join () #     print (' main process ')   #   Each time the main process is first printed, because CPU performance is low #  daemon Def piao (name):     print ('%s is  Piaoing '  % name)     time.sleep (Random.randint (1, 3))      print ('%s is piao&Nbsp;end '  % name) if __name__ ==  ' __main__ ':    p1 =  Process (target=piao, args= (' Egon ',))     p1.daemon = True  #  When the master process finishes running, the child process is finished and will be recycled     p1.start ()     print (p1.pid)      print (P1.name)     print (p1.is_alive ())   # True     p1.terminate ()   #  kill P1, zombie process     time.sleep (0.5)      print (P1.is_alive ())   # False  cannot immediately kill process     print (' main process ')
Simulation of process synchronization grab ticket
from multiprocessing import process, lockimport json, time, randomdef  Work (Dbfile, name, lock):     # lock.acquire ()   #  lock, who gets it first, Other processes are not available, locking is concurrency, join IS queued     with lock:  #  context management          with open (dbfile, encoding= ' utf-8 ')  as f:             dic = json.loads (F.read ())              if dic[' Count '] > 0:                 dic[' Count '] -= 1                 time.sleep ( Random.randint (1,&NBSP;3)   #  Analog Network latency                  with open (dbfile,  ' W ',  encoding= ' utf-8 ')  as f:                     f.write (Json.dumps ( DIC))                 print ('% S rob the ticket success!!! '  % name)             else:                 print ('%s Rob ticket failed!!! '  % name)                  # lock.release ()   #  lock to release, otherwise the process cannot use if __name__ ==  ' __main__ ':     lock = lock ()     p_l = []    for  i in range (+):         p = process (target= Work, args= (' A.txT ',  ' user%s '  % i, lock)         p_l.append (p)          p.start () For p in p_l:    p.join ( ) print (' main process ')
Producer Consumer Model
from multiprocessing import process, joinablequeueimport random, timedef  Consumer (q, name):    while true:         # time.sleep (Random.randint (1, 3))         res =  q.get ()   #  has been taking the status         q.task_done ()    #  Execution Completion         # if res is None:         #     break         print (' \033[41m consumer%s got%s\033[0m '  %  (name, res)) Def producer (Seq, q,  name):    for item in seq:         # time.sleep (Random.randint (1, 3))         q.put (item)         print (' \033[42m producer%s got%s\033[0m '  %  (name, item))     #  q.put (None)     q.join ()     print (' **************>> ')    #  Bank Execution instructions The producer executes, the consumer carries out all Tasks if __name__ ==  ' __main__ ':     q =  joinablequeue ()     c = process (target=consumer, args= (q,  ' LH '))     c.daemon=True  #设置守护进程, Zhu Jincheng ends, C ends     c.start ()      seq = [' bun%s '  % i for i in range (Ten)]     p = process (target=producer, args= (seq, q,  ' Chef 1 '))      P.start ()   #  producer Process     p.join ()   #  main process wait P end, p wait for C to finish the data, C once the data is taken , P.join is not blocking, and then chasing the process to end     #&nbsp, the main process end will recycle the daemon C, and at this time C also does not exist necessary as long as the join producer on the line, the producers are waiting for consumers to take away      # c.joIn ()     print (' main process ') # [i for i in range (Ten)]  list generation # [i  for i in range ( if i > 5] ) List generation


This article is from the "11860872" blog, please be sure to keep this source http://11870872.blog.51cto.com/11860872/1943642

Python Multi-process

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.