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

Use of Python multiprocesses

編輯:Python

1. Computing intensive, multi process ;
2. Execution between processes is unordered , Which process is scheduled by the operating system , Which process executes first ;
3. Global variables are not shared between processes , When a child process is created, all the resources of the parent process are copied ;
4. The main process will wait for the execution of all sub processes to end ;
5. When the main process ends, the child process ends immediately :1. Subprocess object .daemon=True,2. Subprocess object .terminate();
6. Only when executing thread GIL lock ——Global Interpreter Lock .

'''
Computing intensive, multi process
Execution between processes is unordered , Which process is scheduled by the operating system , Which process executes first
Global variables are not shared between processes , When a child process is created, all the resources of the parent process are copied
The main process will wait for the execution of all sub processes to end
When the main process ends, the child process ends immediately :1. Subprocess object .daemon=True,2. Subprocess object .terminate()
Only when executing thread GIL lock ——Global Interpreter Lock
'''
import multiprocessing
import time
import os
list=[]
def task1(i):
# time.sleep(1)
print(' Mission 1:',i)
print(' Mission 1 Address ',id(multiprocessing.current_process()))
print(' Mission 1 Of list Address :',id(list))
print(' Mission 1 name :',multiprocessing.current_process().name) # The name of the current process
print(' Mission 1pid:',multiprocessing.current_process().pid) # Of the current process pid
# print(' Mission 1pid:',os.getpid()) # Of the current process pid
print(' Mission 1 The parent process pid:',os.getppid()) # Of the parent process of the current process pid
print(' Mission 1 Are you alive :',multiprocessing.current_process().is_alive()) # Is the current process alive
def task2(i):
# time.sleep(1)
print(' Mission 2:',i)
print(' Mission 2 Address ', id(multiprocessing.current_process()))
print(' Mission 2 Of list Address :', id(list))
print(' Mission 2 name :',multiprocessing.current_process().name) # The name of the current process
print(' Mission 2pid:',multiprocessing.current_process().pid) # Of the current process pid
# print(' Mission 2pid:',os.getpid()) # Of the current process pid
print(' Mission 2 The parent process pid:',os.getppid()) # Of the parent process of the current process pid
print(' Mission 2 Are you alive :',multiprocessing.current_process().is_alive()) # Is the current process alive
if __name__ == '__main__':
print(f' Of the main process pid yes {os.getpid()}',f' Of the parent process of the main process pid yes {os.getppid()}')
# Get the current process object
mp=multiprocessing.current_process()
print(' Current main process name ',mp.name)
print(' Address of the main process ',id(mp))
print(' Of the main process list Address :', id(list))
print('start')
p1=multiprocessing.Process(target=task1,name='task1',args=(1,))
p2=multiprocessing.Process(target=task2,name='task2',args=(2,))
# p1.daemon=True # Set up daemons , When the main process ends, the child process p1 It's going to end immediately , Must be written to start front
p1.start()
# p1.terminate() # end p1 process
p1.join() # The subprocess p1 Before joining the next process ,p1 The following processes will not be executed until the process is completed
p2.start()
# No addition join, Will be executed directly to stop, Then wait for the process to end
p2.join() # The subprocess p2 Before joining the next process ,p2 The following processes will not be executed until the process is completed
print('stop')


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