[英]Run the same python file multiple times with different input values
I have a process that will take input and then run the in background and evaluate infomation (assuming the process never ends) which means the file basically "stops" I am not able to motify any of the variables.我有一个进程将接受输入,然后在后台运行并评估信息(假设进程永远不会结束),这意味着文件基本上“停止”我无法对任何变量进行主题化。 My process has "stages" and these "stages" require the same evaluation but different input and since I am not able to motify the variables, I am left with making another python file and then changing the variables, and then running that.我的流程有“阶段”,这些“阶段”需要相同的评估但不同的输入,因为我无法对变量进行主题化,所以我只能制作另一个 python 文件,然后更改变量,然后运行它。 My process has to be ran by file, and cannot be defined as a function or loop.我的进程必须按文件运行,不能定义为 function 或循环。
test1.py测试1.py
from .test import testing #-- my manager to calculate the variable data returns a list
value = 473
drones = testing(473) #- returns something like [[0,1,2,3], [4,5,6,7]]
while True: #- The loop in a nutshell, but cannot be defined as a loop or function
process(drones[0]) #- process is the process in a nutshell
Note: Line 1 from.test import testing
is a manager I have made to divide input to data for my process.注意:第 1 行from.test import testing
是我为我的流程将输入划分为数据的管理器。
Note: Line 4 drones = testing(473)
returns a list of lists;注意:第 4 行drones = testing(473)
返回列表列表; each value in the list is the necessary data for one process.列表中的每个值都是一个进程的必要数据。
Note: Line 5 while True:
this is my loop in a nutshell this is not how it is actually handled*注意:第 5 行while True:
简而言之,这是我的循环,这不是它的实际处理方式*
test2.py测试2.py
from .test import testing
value = 473
drones = testing(473)
while True:
process(drones[1])
This is different from test1.py in line 6 process(drones[1])
.这与第 6 行process(drones[1])
的 test1.py 不同。 The data I'm using for my process is different in test1.py ( process(drones[0])
)我用于我的流程的数据在 test1.py ( process(drones[0])
)中有所不同
But what if I have hundreds, maybe thousands?但是,如果我有数百甚至数千呢? I wouldn't just make individual files for that.我不会为此制作单独的文件。
I am open to all answers.我对所有答案持开放态度。 These answer do not have to be purely python (bash, etc.).这些答案不必纯粹是 python (bash 等)。
you could use multiprocessing.Pool
:你可以使用multiprocessing.Pool
:
from multiprocessing import Pool
from .test import testing
value = 473
drones = testing(value)
def proc(lst):
# do something with lst asynchonously
if __name__ == "__main__":
pool = Pool(2) # or however many times needed
pool.map(proc, drones, chunksize=1) # runs proc(drones[0]), proc(drones[1]), ..., each in their own process
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.