python中基于multiprocessing多进程创建方法解析
June 15, 2015
在python中我们如何来通过multiprocessing 来创建多个进程,在下面的文章里将会对python中的multiprocessing 模块做一个介绍,并通过实例来演示如何基于multiprocessing 模块来创建出不同的进程:
本文实例讲述了python基于multiprocessing的多进程创建方法。分享给大家供大家参考。具体如下:
import multiprocessing
import time
def clock(interval):
while True:
print ("the time is %s"% time.time())
time.sleep(interval)
if __name__=="__main__":
p = multiprocessing.Process(target=clock,args=(15,))
p.start() #启动进程
定义进程的另一种方法,继承Process类,并实现run方法:
import multiprocessing
import time
class ClockProcessing(multiprocessing.Process):
def __init__(self, intverval):
multiprocessing.Process.__init__(self)
self.intverval = intverval
def run(self):
while True:
print ("the time is %s"% time.time())
time.sleep(self.interval)
if __name__=="__main__":
p = ClockProcessing(15)
p.start() #启动进程
python中基于multiprocessing多进程创建方法解析就是这样,欢迎大家参考。。。。
0 Comments