首页 > 编程语言 > 详细

python多进程(两种方法)

时间:2016-03-10 12:33:07      阅读:227      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!