DSPRiverInspection/code_bak/test_multiprocess.py

31 lines
899 B
Python

import multiprocessing
def heavy_load_func(N, child_conn):
'''function do heavy computing'''
try:
#do_some_heavy_computing
return_value=99
fp=open('wangjin.txt','r')
fp.close()
child_conn.send(return_value) #return something
except Exception as e:
child_conn.send(e) #将异常通过管道送出
if __name__=='__main__':
'''main function'''
try:
parent_conn, child_conn = multiprocessing.Pipe()
child_process = multiprocessing.Process(target=heavy_load_func, args=(10, child_conn))
child_process.start()
#child_process.join()
child_return = parent_conn.recv()
print('#####main try:',child_return,type(child_return),dir(child_return),child_return.args)
print(str( child_return ))
except Exception as e:
print('####main exception:',e)