简体   繁体   English

如何使用配置文件以不同的设置多次运行python文件?

[英]How to use a config file to run a python file multiple times with different settings?

I have one python script with it's settings in one seperate json config file . 我在一个单独的json配置文件中有一个python脚本及其设置。 The json file looks like this: json文件如下所示:

{
  "connection" : {
      "db_server" : "server",
      "db_name" : "table1", 
      "db_user" : "user1", 
}}

Now I need to run the same python file more than one time, each with other settings in the config file. 现在,我需要多次运行同一个python文件,每个配置文件中都有其他设置。 The other settings would look like this: 其他设置如下所示:

{
  "connection" : {
      "db_server" : "server",
      "db_name" : "table2", 
      "db_user" : "user2", 
}}

I don't need to change anything in the Python script. 我不需要更改Python脚本中的任何内容。 I open the json file in my Python script like this: 我在Python脚本中打开json文件,如下所示:

with open('settings.json') as json_data_file:
    data = json.load(json_data_file)
    json_data_file.close()

Since you cannot add comments in a json file, I don't know the easiest way to do this. 由于您无法在json文件中添加评论,因此我不知道执行此操作的最简单方法。 I want the Python script to run simultaneously two times, each time with other settings for the json file. 我希望Python脚本同时运行两次,每次与json文件的其他设置一起运行。

Thanks in advance! 提前致谢!

  • After launching the the python script, you can just modify the config file inplace and run it a second time. 启动python脚本后,您可以就地修改配置文件并再次运行它。 This won't affect the already-running python program because they already read the config. 这不会影响已经在运行的python程序,因为他们已经读取了配置。
  • Or you can have multiple config files with different names, and run the script with some command line argument (ie sys.argv[1] ) to choose which config file to use. 或者,您可以具有多个名称不同的配置文件,然后使用某些命令行参数(即sys.argv[1] )运行脚本以选择要使用的配置文件。 I personally recommend this approach. 我个人推荐这种方法。

A simple solution is a new script that parses your JSON file, imports your Python scripts, and then executes that scripts with different parameters using concurrent . 一个简单的解决方案是使用新脚本来解析您的JSON文件,导入您的Python脚本,然后使用并发命令以不同的参数执行这些脚本。

An example (adapted from the example for ThreadPoolExecutor) : 一个示例(改编自ThreadPoolExecutor的示例)

import concurrent.futures
import json
from YourModule import MainFunction

# First, open and load your JSON file
parameter_dict = json.load(open('yourfilename.json'))

# Do some parsing to your parameters in order
# (In your case, servers, tables, and users)
parameters_to_submit = [[entry['db_server'], entry['db_table'], entry['db_user'] for entry in parameter_dict.values()]

# Now, generate a ThreadPool to run your script multiple times
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
    # Submit the function + parameters to the executor
    submitted_runs = {
        executor.submit(MainFunction, params[0], params[1], params[2]): params
        for params in parameters_to_submit
    }

    # as the results come in, print the results
    for future in concurrent.futures.as_completed(submitted_runs):
        url = submitted_runs[future]
        try:
            data = future.result()
        except Exception as exc:
            print(f'Script generated an exception: {exc}')
        else:
            # if need be, you could also write this data to a file here
            print(f'Produced result {data}')

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

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