Python多进程使用方法

本文介绍了Python的多进程使用方法,适合初学者了解和学习。通过实例解析,帮助读者掌握如何在Python中实现多进程并进行有效的问题排查。

# -*- coding: utf-8 -*-
"""
Created on Mon May 15 10:27:05 2017

@author: 仗剑天涯
"""
# two methods of multiprocessing on Windows/Unix/Linux and one method on Unix/Linux
# specific: multiprocessing package, multiprocessing pool and fork method

import random
import numpy as np
import datetime
import multiprocessing

# matrix multiplication, matrix a multiplication matrix b and the answer saved matrix c
def matrix_multiplication(index):
    Max = 10
    multiple = 10000
    a = np.zeros((Max,Max))
    b = np.zeros((Max,Max))
    c = np.zeros((Max,Max))
    print 'ending of initialization------',index
    
    for i in range(Max):
		for j in range(Max):
			a[i][j] = random.random()*multiple
			b[i][j] = random.random()*multiple
    print 'ending of random ------',index
    
    for i in range(Max):
		for j in range(Max):
			for k in range(Max):
				c[i][j] += a[i][k] * b[k][j]
    print 'ending of matrix multiplication ------',index
       

def my_multiprocessing(index):
    print index
    matrix_multiplication(index)
	

if __name__ == '__main__':
    now = datetime.datetime.now()
    print now
    
    
    # method 1  using multiprocess which works on Windows or Unix/Linux
    pros = []
    for i in range(10):
        pros.append(multiprocessing.Process(target=my_multiprocessing,args=(i,)))
    for pro in pros:
        pro.start()
    for pro in pros:
        pro.join()
    print 'All subprocesses done.'
    print 'main process cost time is ',datetime.datetime.now() - now
    
    
    # 方式2:如果要启动大量的子进程,可以用进程池的方式批量创建子进程
    # method 2  using multiprocess pool which is suitable for creating a batch of processes
    p = multiprocessing.Pool(4) # 4 means the number of kernel of running process's computer
    for i in range(10):
        p.apply_async(my_multiprocessing, args=(i,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')
    print 'main process cost time is ',datetime.datetime.now() - now 
    
    
# on Unix/Linux/Mac, we can also use fork() method which only works on Unix/Linux/Mac as following:
import os

print('Process (%s) start...' % os.getpid())
# Only works on Unix/Linux/Mac:
pid = os.fork()
if pid == 0:
    print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
else:
    print('I (%s) just created a child process (%s).' % (os.getpid(), pid))


作者能力有限,初学Python,难免会存在错误,欢迎大家学习交流、批评指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值