链接Python脚本

问题描述 投票:0回答:1

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

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

我搜索了一下这个话题;

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

我认为我设计的是错误的,但我无法想出更好的方法。

A more detailed explenation(maybe gibberish)

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

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

python python-3.5 benchmarking
1个回答
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])
#-----------------------------------------------------------------------------
© www.soinside.com 2019 - 2024. All rights reserved.