首页 > 解决方案 > 如何创建多个 Python 脚本并同时运行它们?

问题描述

我有一个 Python 脚本,用于在从各个方向暴露在风中时找到结构(起重机吊臂)上的一些应力。这意味着它会创建 360 个文本文件,结构所面临的每个度数对应 1 个。我不想在单个内核上运行 360 个连续循环,而是想将任务分解为 10 或 20 个进程。有没有办法我可以修改以下代码,以便它创建并运行具有不同度数范围的多个脚本,即一个脚本会做 0 到 20 度,接下来是 20 到 40 等等?

import math
import csv
boomDirection = 0
time = 0
maxStress = 650
with open("SomeWindAndHeadingStressMatrix.csv") as f:
    data = [row for row in csv.reader(f)]    

while boomDirection < 361:
    file = open("SomeWindSpeedSourceData.txt", 'r')
    data_file = open("Bolt Stress - " + str(boomDirection) + " Degrees.csv", 'w')
    line = file.readline()

    while line != '':
        try:
            if len(line.split(','))>1:                                      
                windSpeedHigh = int(int(line.split(',')[19])*1.32)
                windSpeedLow = int(int(line.split(',')[22])*1.32)
                windDirection = int(line.split(',')[14]) - boomDirection
                if windDirection < 0:
                    windDirection += 360
                stressHigh = float(data[windSpeedHigh][windDirection])
                stressLow = float(data[windSpeedLow][windDirection])
                if time % 10080 == 0:
                    data_file.write(str(time) + ', ' + str(maxStress) + ('\n'))
                    time += 0.5
                else:
                    data_file.write(str(time) + ', ' + str(round(stressHigh,1)) + ('\n'))
                    time += 0.5
                data_file.write(str(time) + ', ' + str(round(stressLow,1)) + ('\n'))
                time += 0.5                    
        except ValueError:
            pass
        line = file.readline()
    data_file.close()
    time = 0
    boomDirection = boomDirection + 1

标签: python-3.xmultiprocessing

解决方案


推荐阅读