码迷,mamicode.com
首页 > 编程语言 > 详细

python多进程(两种方法)

时间:2016-03-10 12:36:25      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from multiprocessing import Pool
import time

def f(x):
    time.sleep(1)
    print x
    return  x*x

if __name__ == ‘__main__‘:
    p = Pool(5)
    print(p.map(f,range(10)))

 

from multiprocessing import Process
import os

def info(title):
    print title
    print ‘module name:‘, __name__
    if hasattr(os, ‘getppid‘):  # only available on Unix
        print ‘parent process:‘, os.getppid()
    print ‘process id:‘, os.getpid()

def f(name):
    info(‘function f‘)
    print ‘hello‘, name

if __name__ == ‘__main__‘:
    info(‘main line‘)
    p = Process(target=f, args=(‘bob‘,))
    p.start()
    p.join()

 

 

#显示多进程之间不进行通信 
#显示多进程之间不进行通信
from multiprocessing import Process

def run(info_title,n):
    info_title.append(n)
    print info_title

info_title = []
if __name__ == ‘__main__‘:
    for i in range(10):
        p = Process(target=run,args=(info_title,i))
        p.start()

############显示结果为############
[1]
[0]
[4]
[6]
[8]
[7]
[2]
[9]
[5]
[3]

 

python多进程(两种方法)

标签:

原文地址:http://www.cnblogs.com/fengjian2016/p/5261246.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!