python的多线程有点鸡肋了,所以最近学了些多进程的东西,记录下!!
multiprocessing.Process(self, group=None, target=None, name=None, args=(), kwargs={})
其方法有:
is_alive(self)
Return whether process is alive
join(self, timeout=None)
Wait until child process terminates
run(self)
Method to be run in sub-process; can be overridden in sub-class
start(self)
Start child process
terminate(self)
Terminate process; sends SIGTERM signal or uses TerminateProcess()
多进程的使用方法跟多线程是类似的,先看个例子:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import multiprocessing
import time
def first_func(i):
num = 3
while num > 0:
print "now at:",time.ctime()
time.sleep(i)
num -=1
if __name__ == "__main__":
mp = multiprocessing.Process(target=first_func, args=(2,))
mp.start()
print "pid:",mp.pid
print "name:",mp.name
print "is_alive:",mp.is_alive()
pid: 63398
name: Process-1
is_alive: True
now at: Mon Nov 28 16:22:42 2016
now at: Mon Nov 28 16:22:44 2016
now at: Mon Nov 28 16:22:46 2016