import os
import numpy as np
import time
from multiprocessing import Process, current_process
def doubler(number):
result = number * 2
proc_name = current_process().name
print('{0} doubled to {1} by: {2}'.format( number, result, proc_name))
def solve_inverse(np_ndarray_square_matrix):
inverse_matrix=np.linalg.inv(np_ndarray_square_matrix)
proc_name = current_process().name
print('process name :',proc_name,' ',inverse_matrix)
if __name__=='__main__':
start_time=time.time()
dim=100
thread_num=10
matrice = [np.random.normal(loc=1.0 , scale=5.0 , size=(dim,dim)) for _ in range(thread_num)]
procs = []
for index, matrix in enumerate(matrice):
proc = Process(target=solve_inverse , args=(matrix,))
procs.append(proc)
proc.start()
for proc in procs:
proc.join()
end_time=time.time()
print('time length :',end_time-start_time)
the code above is a simple python code which computes inverse of randomly sampled matrices with multiprocessing. However , the following code doesn't work
import os
import numpy as np
import time
from multiprocessing import Process, current_process
def doubler(number):
result = number * 2
proc_name = current_process().name
print('{0} doubled to {1} by: {2}'.format( number, result, proc_name))
def solve_inverse(np_ndarray_square_matrix):
inverse_matrix=np.linalg.inv(np_ndarray_square_matrix)
proc_name = current_process().name
print('process name :',proc_name,' ',inverse_matrix)
start_time=time.time()
dim=3
thread_num=10
matrice = [np.random.normal(loc=1.0 , scale=5.0 , size=(dim,dim)) for _ in range(thread_num)]
procs = []
for index, matrix in enumerate(matrice):
proc = Process(target=solve_inverse , args=(matrix,))
procs.append(proc)
proc.start()
for proc in procs:
proc.join()
end_time=time.time()
print('time length :',end_time-start_time)
the only difference is whether there is if __name__=='__main__:
or not.
As far as I know, if __name__=='__main__:
recognize if this module is imported by the other module or this module run itself. So I thought actually there's no difference between two codes to decide what computer should do. what's wrong?