python版本是3.5.6
# main.py #
import os
import sys
import subprocess
from datetime import datetime
import multiprocessing as mp
from util import _gzsl_arg_template, _zsl_arg_template
os.environ['CUDA_VISIBLE_DEVICES'] = '2'
# Experimental Setting
EXP_SET = '/zsl'
MODEL = '/argan'
EXP = '/e3_difD'
MODEL_PATH = './model' + MODEL
EXP_PATH = './exp' + EXP_SET + MODEL + EXP
DATASETS = ['/CUB', '/FLO']
# Arguments to Test
TEST_ARG = '--d_hl'
TEST_VALUE = [1,2,3]
for dataset in DATASETS:
if EXP_SET == '/gzsl':
arg_template = _gzsl_arg_template.get(dataset)
else:
arg_template = _zsl_arg_template.get(dataset)
pool = mp.Pool(processes=len(TEST_VALUE))
for value in TEST_VALUE:
arg_template[TEST_ARG] = value
arg_list = []
for (key,data) in arg_template.items():
arg_list.append(key)
arg_list.append(str(data))
arg_list.insert(0, MODEL_PATH+EXP+'.py')
arg_list.insert(0, 'python3')
result_file = EXP_PATH + dataset + '/value_' + str(value) + '.txt'
with open(result_file, 'w') as r_file:
print('\n##########################################', file=r_file)
print('Exp Info: \n\t type: {0}\n\t model: {1}\n\t exp: {2}\n\t time: {3}'.format('generalized zero-shot', MODEL, EXP, datetime.now().strftime('%Y-%m-%d %H:%M:%S')), file=r_file)
print('##########################################', file=r_file)
pool.apply_async(func=subprocess.run, kwds=dict(args=arg_list, stdout=r_file))
pool.close()
pool.join()
上面是我要运行的python文件。问题是当我使用python main.py
运行代码时,没有向池中添加任何进程。
我想这些论点肯定有问题。所以我检查了python官方文档的调用规范,但是没有错误。而且,当我只使用subprocess.run(arg_list, stdout=r_file)
时,代码可以正确运行。
我简单地执行以下操作:
def err(e):
print(e)
并替换
apply_async(func=subprocess.run, kwds=dict(args=arg_list, stdout=r_file))
具有:
apply_async(func=subprocess.run, kwds=dict(args=arg_list, stdout=r_file), error_callback=err)
输出:
cannot serialize '_io.TextIOWrapper' object
我无法弄清楚通话有什么问题。帮助