Process([group [, target [, name [, args [, kwargs]]]]])
group: 无用
target: 调用函数
name: 进程名称
args: 参数
kwargs: 参数
方法
start(): 启动线程
join([timeout]): 阻塞
is_alive(): 进程是否在运行
属性
pid: 进程号
name: 进程名称
daemon: 定义是否是守护进程, 定义方法与线程Thread相同
创建进程
import time
import multiprocessing
def worker(interval):
n = 5
while n > 0:
print("The time is {0}".format(time.ctime()))
time.sleep(interval)
n -= 1
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: 1908
p.name: Process-1
p.is_alive: True
The time is Tue Apr 21 20:55:12 2015
The time is Tue Apr 21 20:55:15 2015
The time is Tue Apr 21 20:55:18 2015
The time is Tue Apr 21 20:55:21 2015
The time is Tue Apr 21 20:55:24 2015
import time
import multiprocessing
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
p = ClockProcess(3)
p.start()