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,難免會存在錯誤,歡迎大家學習交流、批評指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章