程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python - multi process

編輯:Python

What is a process :

An application , It all boils down to a pile of code , Is static , The process is the program that is being executed , When a program is running , It is possible that multiple processes are executing concurrently .

Process versus thread comparisons

  • Process is the basic unit of system resource allocation .
  • A process can contain multiple threads , Belong to one to many , All resources of the process , Shared by threads within it .
  • Threads are the smallest unit , If a process completes a function , So thread is the basic unit of this function .
  • Resources between processes are not shared , Multi process consumption is high , Difficulty ; All threads in the same process enjoy resource sharing , Low multi threading consumption , Difficulty is small .

Similarities between processes and threads : To improve the efficiency of the program , Have the priority of implementation .

Multi process :

from multiprocessing import Process
def foo(name):
print('hello ,%s'%name)
if __name__ == '__main__':
p1 = Process(target=foo,args=('world!',))
p2 = Process(target=foo,args=('Python!',))
p1.start()
p2.start()
print(' The main process ')


The main process and child processes execute concurrently
Be careful : Process The object can only be in if name == ‘main’: Create , Otherwise, it will report a mistake .

Process Built in method of :

p.start Start the process , And call... In the child process p.run()p.run() How to run a process when it starts , It is it that calls target function , Our custom class must implement this method p.terminate() Force end process p, No cleaning operations will be carried out , If p Create child process , This sub process becomes a zombie process . If p If a lock is retained, it will not be released , Which leads to deadlock p.is_alive If it is still running , Then return to Truep.join([timeout]) Main thread waiting p suspend ,timeout Optional timeout for

Process attribute :

p.daemon The default is False, If it is set to True, representative p Daemons running in the background , When p When the parent process of is terminated ,p Also terminate ,p You can't create your own new process yet , Must be in p.start() Set before p.name The name of the process p.pid Process pidp.exitcode The process at run time is None, If -N, To signal N end


One has at least one dream , There's a reason to be strong . If the heart has no place to rest , I'm wandering everywhere .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved