The result of tag: (1 results)

Python多进程编程之Process

by LauCyun Aug 14,2016 12:13:34 10,358 views

利用multiprocessing.Process对象可以创建一个进程,该Process对象与Thread对象的用法相同,也有start()run()join()等方法。Process类适合简单的进程创建,如需资源共享可以结合multiprocessing.Queue使用;如果想要控制进程数量,则建议使用进程池Pool类。

构造方法:

class multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

  • target表示调用对象
  • args表示调用对象的位置参数元组
  • kwargs表示调用对象的字典
  • name为别名
  • group实质上不使用

方法:

  • is_alive():返回进程是否在运行
  • join([timeout]):阻塞当前上下文环境的进程程,直到调用此方法的进程终止或到达指定的timeout(可选参数)
  • start():进程准备就绪,等待CPU调度
  • run()strat()调用run方法,如果实例进程时未制定传入target,这start()执行默认run()方法
  • terminate():不管任务是否完成,立即停止工作进程

属性:

  • authkey:进程的认证密钥
  • daemon:和线程的setDeamon功能一样(将父进程设置为守护进程,当父进程结束时,子进程也结束),必须在start()之前设置
  • exitcode:进程在运行时为None、如果为–N,表示被信号N结束
  • name:进程名字
  • pid:进程号

使用Process创建子进程

举个栗子1:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing
import time


def worker(interval):
    n = 5
    while n > 0:
        print("The time is {0}".format(time.ctime()))
        time.sleep(interval)
        n -= 1


if __name__ == "__main__":
    p = multiprocessing.Process(target=worker, args=(3,))
    p.start()
    print("p.pid:", p.pid)
    print("p.name:", p.name)
    print("p.is_alive:", p.is_alive())

输出如下:

p.pid: 6376
p.name: Process-1
p.is_alive: True
The time is Thu Aug 14 09:07:16 2016
The time is Thu Aug 14 09:07:19 2016
The time is Thu Aug 14 09:07:22 2016
The time is Thu Aug 14 09:07:25 2016
The time is Thu Aug 14 09:07:28 2016

栗子2:创建函数并将其作为多个进程

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing
import time


def worker_1(interval):
    print("worker_1")
    time.sleep(interval)
    print("end worker_1")


def worker_2(interval):
    print("worker_2")
    time.sleep(interval)
    print("end worker_2")


def worker_3(interval):
    print("worker_3")
    time.sleep(interval)
    print("end worker_3")


if __name__ == "__main__":
    p1 = multiprocessing.Process(target=worker_1, args=(2,))
    p2 = multiprocessing.Process(target=worker_2, args=(3,))
    p3 = multiprocessing.Process(target=worker_3, args=(4,))

    p1.start()
    p2.start()
    p3.start()

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))
    for p in multiprocessing.active_children():
        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))
    print("END!!!!!!!!!!!!!!!!!")

输出结果如下:

The number of CPU is:8
child: p.name=Process-1, p.id=7872
child: p.name=Process-2, p.id=10872
child: p.name=Process-3, p.id=132
END!!!!!!!!!!!!!!!!!
worker_1
worker_3
worker_2
end worker_1
end worker_2
end worker_3

使用Process类继承创建子进程

现在将栗子1修改一下,将进程定义为类,如栗子3:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing
import time


class ClockProcess(multiprocessing.Process):
    def __init__(self, interval):
        multiprocessing.Process.__init__(self)
        self.interval = interval

    def run(self):
        n = 5
        while n > 0:
            print("the time is {0}".format(time.ctime()))
            time.sleep(self.interval)
            n -= 1


if __name__ == '__main__':
    p = ClockProcess(3)
    p.start()  # 进程p调用start()时,自动调用run()
    print("p.pid:", p.pid)
    print("p.name:", p.name)
    print("p.is_alive:", p.is_alive())

输出结果为:

p.pid: 1004
p.name: ClockProcess-1
p.is_alive: True
the time is Thu Aug 14 09:31:40 2016
the time is Thu Aug 14 09:31:43 2016
the time is Thu Aug 14 09:31:46 2016
the time is Thu Aug 14 09:31:49 2016
the time is Thu Aug 14 09:31:52 2016

如何使用daemon

最后,来讲一讲daemon,它是进程的守护神,当它退出时,它会尝试终止其所有的守护进程子进程。但是必须在调用start()之前设置。下面举栗子来说明:

栗子4.1:不加daemon属性

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing
import time


def worker(interval):
    print("work start:{0}".format(time.ctime()))
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()))


if __name__ == "__main__":
    p = multiprocessing.Process(target=worker, args=(3,))
    p.start()
    print("end!")

输出结果为:

end!
work start:Thu Aug 14 09:38:36 2016
work end:Thu Aug 14 09:38:39 2016

栗子4.2:使用daemon属性

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing
import time


def worker(interval):
    print("work start:{0}".format(time.ctime()))
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()))


if __name__ == "__main__":
    p = multiprocessing.Process(target=worker, args=(3,))
    p.daemon = True
    p.start()
    print("end!")

输出结果为:

end!

子进程没起作用,不可思议吧!那是因为它结束时,它会尝试终止其所有的守护进程子进程。

栗子4.3:设置daemon执行完结束的方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing
import time


def worker(interval):
    print("work start:{0}".format(time.ctime()))
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()))


if __name__ == "__main__":
    p = multiprocessing.Process(target=worker, args=(3,))
    p.daemon = True
    p.start()
    p.join()
    print("end!")

输出结果为:

work start:Thu Aug 14 09:45:24 2016
work end:Thu Aug 14 09:45:27 2016
end!

...

Tags Read More