简体   繁体   中英

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. My process has to be ran by file, and cannot be defined as a function or loop.

test1.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.

  • Note: Line 4 drones = testing(473) returns a list of lists; 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*

test2.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]) . The data I'm using for my process is different in 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.).

you could use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM