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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

pytorch获取模型某一层参数名及参数值方式

1、Motivation: I wanna modify the value of some param; I wanna check the value of some param....

详解Python中for循环是如何工作的

详解Python中for循环是如何工作的

前言 for...in 是Python程序员使用最多的语句,for 循环用于迭代容器对象中的元素,这些对象可以是列表、元组、字典、集合、文件,甚至可以是自定义类或者函数,例如: 作用于列...

JupyterNotebook设置Python环境的方法步骤

JupyterNotebook设置Python环境的方法步骤

使用Python时,常遇到的一个问题就是Python和库的版本不同。Anaconda的env算是解决这个问题的一个好用的方法。但是,在使用Jupyter Notebook的时候,我却发现...

在Python中进行自动化单元测试的教程

在Python中进行自动化单元测试的教程

一、软件测试 大型软件系统的开发是一个很复杂的过程,其中因为人的因素而所产生的错误非常多,因此软件在开发过程必须要有相应的质量保证活动,而软件测试则是保证质量的关键措施。正像软件熵(so...

python实现的DES加密算法和3DES加密算法实例

本文实例讲述了python实现的DES加密算法和3DES加密算法。分享给大家供大家参考。具体实现方法如下: #####################################...