Home > Backend Development > Python Tutorial > python基于multiprocessing的多进程创建方法

python基于multiprocessing的多进程创建方法

WBOY
Release: 2016-06-06 11:18:03
Original
1163 people have browsed it

本文实例讲述了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() #启动进程
Copy after login

定义进程的另一种方法,继承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() #启动进程
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template