【Python】process 模块的其他方法

import time,os
from multiprocessing import Process

# Process.join() 等待子进程结束
# Process.start() 让子进程执行类里的run方法
# Process.terminate() 终止子进程
# Process.is_alive() 判断子进程是否存活


def run(name, second): # 固定名字run, 会被start调用

print(name, 'child <%s> is running, parent is <%s>' % (os.getpid(), os.getppid()))
time.sleep(second)
print(name, 'child <%s> is done, parent is <%s>' % (os.getpid(), os.getppid()))


if __name__ == '__main__':
p1 = Process(target=run, kwargs={'name': 'p1', 'second': 3})
p2 = Process(target=run, kwargs={'name': 'p2', 'second': 2})
p3 = Process(target=run, kwargs={'name': 'p3', 'second': 1})
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
print('child <%s> still exist ' % p1.pid)
print('主进程<%s>, pycharm<%s>' % (os.getpid(), os.getppid()))
posted @ 2018-08-26 17:45  caya  阅读(777)  评论(0编辑  收藏  举报