因为要用到python的多进程测试个东西,所以特此记录一下:
import pandas as pd
import multiprocessing
import time
import traceback
def read_data(path):
try:
data = pd.read_csv(path,encoding='utf-8')
time.sleep(2)
print(data)
print('-------------')
except Exception as e:
raise e
return True
if __name__ == '__main__':
pool = multiprocessing.Pool(processes = 2)
path_list = []
path_list.append("/Users/code/python code/data.txt")
path_list.append("")
for i in path_list:
print('begin a pool')
res = pool.apply_async(read_data,(i,))
print(res.get())
pool.close()
pool.join()
print('end')
python的多进程打印异常和java不太一样,子进程有单独的堆栈信息,所以子进程的异常信息不会在主进程里面打印,所以需要在调用的函数里面抛出,然后在主进程里面接收,捕获异常信息。