from multiprocessing import Process
import time
def f(name):
time.sleep(1)
print("hello",name,time.ctime())
if __name__ == '__main__':
p_list=[]
for i in range(3):
p=Process(target=f,args=('alvin',))
p_list.append(p)
#p.daemon=True
p.start()
print('end....')
多进程模式:是由一个主进程创建多个其他进程,因此其他进程的父进程就是这个主进程
####eg1:
from multiprocessing import Process
import time,os
def f(name):
print("hello",name)
print('parent process:' , os.getppid())
print('process:',os.getpid())
if __name__ == '__main__':
p_list=[]
for i in range(3):
p=Process(target=f,args=('alvin',))
p_list.append(p)
#p.daemon=True
p.start()
###执行结果####
hello alvin
parent process: 20308
process: 19656
hello alvin
parent process: 20308
process: 5688
hello alvin
parent process: 20308
process: 9344
#######eg2###########
from multiprocessing import Process
import time,os
def info(title):
print('title:',title)
print('parent process:' , os.getppid())
print('process:',os.getpid())
def f(name):
info('function f')
print("hello",name)
if __name__ == '__main__':
info('main process line')
time.sleep(1)
p=Process(target=info,args=('alvin',))
p.start()
###执行结果####
title: main process line
parent process: 1644
process: 15848
title: alvin
parent process: 15848
process: 3348