简体   繁体   English

我如何在 linux 的不同时间为不同的输入运行相同的 python 脚本

[英]how do i run same python script for different inputs at different time in linux

I have one python script which works for one input at a time.我有一个 python 脚本,一次可用于一个输入。 I have a list of inputs and I want this python script to run for different inputs independently in Linux.我有一个输入列表,我希望这个 python 脚本在 Linux 中独立运行不同的输入。

Suppose this is my script:假设这是我的脚本:

script.py with input "name1", 
list of inputs = ["name1", "name2"]

Using for loop inside the script does not satisfy my requirement.在脚本中使用 for 循环不能满足我的要求。 Is this possible using subprocess?这可以使用子流程吗? or a for loop in another python file?还是另一个 python 文件中的 for 循环? If yes, then how?如果是,那么如何? I tried running this script.py using another python file with code:我尝试使用另一个带有代码的 python 文件运行此 script.py:

from subprocess import call

for i in range(len(list_inputs)): ???

   call(["python", "script.py"])

but how do I give input to my script.py, also I do not want to give input by typing on terminal但是我如何给我的script.py输入,我也不想通过在终端上输入来输入

Please suggest how do I do that?请建议我该怎么做?

You can run functions wtih multiprocessing in python with appropriate inputs.您可以使用适当的输入在 python 中运行具有多处理功能的函数。 Like passing function arguments.就像通过 function arguments。 But keep that in mind, you cannot pass global variables to subprocesses due to their nature(they dont share memory with parent-child processes).但请记住,由于它们的性质,您不能将全局变量传递给子进程(它们不与父子进程共享 memory)。 You can find an example usage below:您可以在下面找到示例用法:

# Your desired function
def example_func(a, b, c):
    while True:
        print a
        print b
        print c

# Creating the child process
child_process = multiprocessing.Process(target=example_func, args=(a, b, c))

# Starting child process
child_process.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 linux 中与不同的参数并行运行 python 脚本? - How do I run a python script in parallel with different arguments in linux? 在具有不同输入的 1x exe 中同时多次运行 python 脚本 - run python script multiple times at the same time in 1x exe with different inputs 如何通过Python脚本使用不同的输入运行多次相同的程序? - How to run several times the same program with different inputs via Python script? 如何同时在 Python 中运行两个不同的代码? - How can I run two different code in Python at the same time? 使用不同的输入多次运行 Python 脚本 - Run Python script multiple times with different inputs 如何每天使用调度程序同时运行python脚本? - How do I run python script everyday at the same time with scheduler? 如何同时使用python运行2个不同的进程? - How to run 2 different processes with python at the same time? 如何从python中的不同输入打印到CSV文件 - how do i print to a a CSV file from different inputs in python 安排相同的方法来运行对应于其时间间隔的不同输入 - Scheduling same method to run for different inputs corresponding to its time interval 为 Gspread 使用 python 脚本时,如何使用不同的 Oauth 令牌运行脚本? - How do I run a script using a different Oauth Token when using a python script for Gspread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM