process对象的其他属性或者方法
Posted yuexijun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了process对象的其他属性或者方法相关的知识,希望对你有一定的参考价值。
#join方法
import time, os
from multiprocessing import Process
def task(name, n):
print(‘%s is running‘%name)
time.sleep(n)
if __name__ ==‘__main__‘:
start = time.time()
p1 = Process(target=task, args=(‘子进程1‘,5))
p2 = Process(target=task, args=(‘子进程2‘,3))
p3 = Process(target=task, args=(‘子进程3‘,2))
p1.start()
p2.start()
p3.start()
p1.join() #此方法写时间不会累计时间(串行)
p2.join()
p3.join()
print(‘主‘, time.time() - start)
print(p1.pid)
print(p2.pid)
print(p3.pid)
import time, os
from multiprocessing import Process
def task(name, n):
print(‘%s is running‘%name)
time.sleep(n)
if __name__ ==‘__main__‘:
start = time.time()
p1 = Process(target=task, args=(‘子进程1‘,5))
p2 = Process(target=task, args=(‘子进程2‘,3))
p3 = Process(target=task, args=(‘子进程3‘,2))
p1.start() #此方法写会累计时间(串行)
p1.join()
p2.start()
p2.join()
p3.start()
p3.join()
print(‘主‘, time.time() - start)
print(p1.pid)
print(p2.pid)
print(p3.pid)
import time, os
from multiprocessing import Process
def task(name, n):
print(‘%s is running‘%name)
time.sleep(n)
if __name__ ==‘__main__‘:
start = time.time()
p1 = Process(target=task, args=(‘子进程1‘,5))
p2 = Process(target=task, args=(‘子进程2‘,3))
p3 = Process(target=task, args=(‘子进程3‘,2))
p_l = [p1, p2, p3]
for p in p_l:
p.start()
for p in p_l:
p.join()
print(‘主‘, time.time() - start)
print(p1.pid)
print(p2.pid)
print(p3.pid)
#了解 is_alive 判断进程是否存活
import time, os
from multiprocessing import Process
def task(name):
print(‘%s is running‘%name)
time.sleep(3)
if __name__ ==‘__main__‘:
start = time.time()
p1 = Process(target=task, args=(‘子进程1‘,))
p1.start() #此方法写会累计时间(串行)
print(p1.is_alive())
p1.join()
print(‘主‘, time.time() - start)
print(p1.pid)
print(p1.is_alive())
import time, os
from multiprocessing import Process
def task(name):
print(‘%s is running‘%name)
time.sleep(3)
if __name__ ==‘__main__‘:
p = Process(target=task, name = ‘我是进程名字‘, args=(‘子进程‘,))
p.start() # 起动进程 (只是向操作系统发了命令 并非执行成功)
p.join() # 等待进程执行成功
p.terminate() #杀死进程
print(p.is_alive()) # 判断进程是否活着
print(p.name) # 查看进程名字
以上是关于process对象的其他属性或者方法的主要内容,如果未能解决你的问题,请参考以下文章