Геометрия, которую я пытаюсь создать, представляет собой пакет прямоугольников. со случайным кругом внутри они не зависят друг от друга. поэтому я хочу использовать многопроцессорный процесс Python для их параллельной генерации.
Имя файла кода — geninp.py, подробности следующие:
Код: Выделить всё
import re
from pathlib import Path
import subprocess
from concurrent.futures import ProcessPoolExecutor
num_elp = 2
samplesize = 100
thisdir = Path(__file__).resolve().parent
template_path = thisdir / 'template_qfnelpinp.py'
def print_runtime_info(proc: subprocess.Popen, proc_comment: str) -> None:
while True:
output = proc.stdout.readline()
if output == b'' and proc.poll() is not None:
break
if output:
print(output.strip().decode())
if proc.stderr:
print(proc.stderr.read().strip().decode())
print('\n' + '-'*25 + proc_comment + ' finished.' + '-'*25 + '\n')
def run_cmd(cmd: str, cmd_comment: str) -> None:
try:
with subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
print_runtime_info(proc, cmd_comment)
proc.wait()
except Exception as e:
print(f'Error while running {cmd}: {e}')
with open(template_path, 'r') as f:
file_content = f.read()
def create_and_run(num_seg, inp_idx):
file_content_new = re.sub(r'{num_seg}', f'{num_seg}', file_content)
file_content_new = re.sub(r'{inum}', f'{inp_idx}', file_content_new)
inp_path = thisdir / f'qfnelpinp{num_seg}-{inp_idx}.py'
with open(inp_path, 'w') as f:
f.write(file_content)
cmd = f'abaqus cae noGUI={inp_path}'
run_cmd(cmd, 'Generate abaqus inp file')
if __name__ == '__main__':
num_segs = [num_elp for _ in range(samplesize)]
odb_idxs = range(samplesize)
with ProcessPoolExecutor(max_workers=1) as executor:
executor.map(create_and_run, num_segs, odb_idxs)
когда я запускаю код с помощью python3 geninp. py, около 40% файлов .inp можно сгенерировать правильно, остальные — нет. Сообщение о сбое: изображение сообщения о сбое
но когда я создаю файл с ошибкой отдельно с помощью abaqus cae noGUI=elp2-i.py(i — метка файла с ошибкой) , это можно сделать правильно. Это означает, что каждый файл elp2-i.py правильный, но он не может быть отправлен одновременно с помощью ProcessPoolExecutor.
Подробнее здесь: https://stackoverflow.com/questions/790 ... ltiprocess