python开启多个子进程并行运行的方法

yipeiwu_com6年前Python基础

本文实例讲述了python开启多个子进程并行运行的方法。分享给大家供大家参考。具体如下:

这个python代码创建了多个process子进程,创建完成后先start(),最后统一join,这样所有子进程会并行执行。

from multiprocessing import Process
import sys, os
import time
def timetask(times):
  time.sleep(times)
  print time.localtime()
def works(func, arg, worknum):
  proc_record = []
  for i in range(worknum):
    p = Process(target = func, args = (arg,))
    p.start()
    proc_record.append(p)
  for p in proc_record:
    p.join()
if __name__ == '__main__':
  arg = 5
  procs = 4
  works(timetask, arg, procs)

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

相关文章

对Python中内置异常层次结构详解

如下所示: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exceptio...

python2.7实现复制大量文件及文件夹资料

需求:拷大量数据,发现有2000G,靠系统的复制功能怕是得好几个小时,于是回来学一手操作,话不多说上代码: 说明:CopyFiles1是可以将sourceDir连子目录一起原样复制到ta...

python中正则表达式 re.findall 用法

Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。 Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正...

python SQLAlchemy的Mapping与Declarative详解

前面介绍过vSQLAlchemy中的 Engine 和 Connection,这两个对象用在row SQL (原生的sql语句)上操作,而 ORM(Object Relational M...

老生常谈Python startswith()函数与endswith函数

函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一、函数说明 语法:string.startswith(str, beg=0,end=len(string)...