【问题标题】:Chaining Python Scripts链接 Python 脚本
【发布时间】:2017-12-31 01:23:33
【问题描述】:

我有两个用户定义的 python 脚本。 First 获取一个文件并对其进行处理,而第二个脚本获取 first 的输出并运行一个可执行文件,并将第一个脚本的输出提供给具有附加格式的程序。

我需要通过另一个 python 脚本运行这些脚本,这是我的主要可执行脚本。

我搜索了一些关于这个主题的信息;

  1. 我可以使用importlib 来收集脚本的内容,以便在适当的时候调用它们。这要求脚本位于我的目录下/或修改路径环境变量。所以它看起来有点难看,看起来不像是蟒蛇。
  2. 内置eval函数。这需要用户编写类似服务器-客户端的结构,因为第二个脚本可能必须多次运行所述程序,而第一个脚本仍然提供输出。

我认为我的设计有问题,但我想不出更好的方法。

更详细的解释(可能是胡言乱语)

我需要对一些程序进行基准测试,同时我有一个标准形式的数据,并且需要将这些数据提供给基准测试程序。这些脚本(由于基准的性质)对每个程序都是特殊的,并且需要与基准定义捆绑在一起,但我需要将此程序创建为独立的可配置测试器。我想,我设计错了,很想听听设计方法。

PS:我不想限制用户,这也是我选择运行python脚本的原因。

【问题讨论】:

    标签: python python-3.5 benchmarking


    【解决方案1】:

    我创建了一些测试脚本来确保它有效。 第一个 (count_01.py) 休眠 100 秒,然后从 0 计数到 99 并将其发送到 count_01.output。 第二个(count_02.py)读取第一个(count_01.output)的输出并将每个数字加1并将其写入count_02.output。 第三个脚本 (chaining_programs.py) 运行第一个脚本并等待它完成,然后再调用第二个脚本。

    # count_01.py --------------------
    from time import sleep
    
    sleep(100)
    
    filename = "count_01.output"
    file_write = open(filename,"w")
    for i in range(100):
        #print " i = " + str(i)
        output_string = str(i)
        file_write.write(output_string)
        file_write.write("\n")
    file_write.close()
    # ---------------------------------
    
    # count_02.py --------------------
    file_in = "count_01.output"
    file_out = "count_02.output"
    file_read = open(file_in,"r")
    file_write = open(file_out,"w")
    for i in range(100):
        line_in = file_read.next()
        line_out = str(int(line_in) + 1)
        file_write.write(line_out)
        file_write.write("\n")
    file_read.close()
    file_write.close()
    # ---------------------------------
    
    # chaining_programs.py -------------------------------------------------------
    import subprocess
    import sys
    #-----------------------------------------------------------------------------
    path_python = 'C:\Python27\python.exe' # 'C:\\Python27\\python.exe'
    #
    # single slashes did not work
    #program_to_run = 'C:\Users\aaaaa\workspace\Rich_Project_044_New_Snippets\source\count.py'
    program_to_run_01 = 'C:\\Users\\aaaaa\\workspace\\Rich_Project_044_New_Snippets\\source\\count_01.py'
    program_to_run_02 = 'C:\\Users\\aaaaa\\workspace\\Rich_Project_044_New_Snippets\\source\\count_02.py'
    #-----------------------------------------------------------------------------
    # waits
    sys.pid = subprocess.call([path_python, program_to_run_01])
    # does not wait
    sys.pid = subprocess.Popen([path_python, program_to_run_02])
    #-----------------------------------------------------------------------------
    

    【讨论】:

    • 感谢您的帮助,但我想我的措辞有误。第二个脚本应该在运行时获取第一个脚本的输出,但是你的代码给了我一个想法。我可能会使用 base64 对应该发送到第二个 python 脚本的数据进行编码,这样我就可以从标准输入读取到空间并对其进行解码。这可能是一个更糟糕的方法,但用户应该少写一些来接收第一个脚本的输出
    • 也许这会有所帮助:stackoverflow.com/questions/33131863/…
    猜你喜欢
    • 1970-01-01
    • 2014-06-13
    • 2015-09-28
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多