保持进程开放并用Python与之通信

保持进程开放并用Python与之通信,python,Python,我正在使用python脚本进行一些迭代。我是这样做的 python exec.py variables.in.1 output.out.1 这里1是迭代次数 现在,它使用变量文件中的值调用可执行文件,然后将可执行文件的输出放入输出文件。不幸的是,我看到的是,反复调用可执行文件需要更多的时间 相反,我想做的是启动可执行文件一次,然后使用相同的语法将变量发送到可执行文件,即python exec.py variables.In.1 output.out.1 我需要保留这种语法是因为变量文件是在外部生

我正在使用python脚本进行一些迭代。我是这样做的

python exec.py variables.in.1 output.out.1

这里1是迭代次数

现在,它使用变量文件中的值调用可执行文件,然后将可执行文件的输出放入输出文件。不幸的是,我看到的是,反复调用可执行文件需要更多的时间

相反,我想做的是启动可执行文件一次,然后使用相同的语法将变量发送到可执行文件,即python exec.py variables.In.1 output.out.1

我需要保留这种语法是因为变量文件是在外部生成的。我无法控制它,但一旦生成它,我需要使用可执行文件来创建输出文件,然后需要以所需的格式对其进行解析

编辑:我将尝试以简化的方式表示我正在做的事情,因为这是一个大型代码

import sys
import os
import parseinput
import parseoutput

s1=sys.argv[1];s2=sys.argv[2]

op=open(s1,'r');inp=op.readlines();op.close()
variableswrite=parseinput.parseinput(inp)
op=open('pass.dat','w');op.write(variableswrite);op.close()

os.sytem('execute < pass.dat')

op=open('executeout.dat','r');out=op.readlines();op.close()
outwrite=parseoutput.parseoutput(out)
op=open(s2,'w');op.write(outwrite);op.close()
导入系统 导入操作系统 导入解析输入 导入解析输出 s1=sys.argv[1];s2=sys.argv[2] op=开启(s1,'r');inp=op.readlines();op.close() variableswrite=parseinput.parseinput(inp) op=打开('pass.dat','w');op.write(变量写入);op.close() 操作系统('execute 这是一个非常粗糙的方式来显示我在做什么。如果这是exec.py代码,每次我使用python exec.py变量.in.1 results.out.1运行它时,我都会使用可执行文件获取一个输出文件,该文件将被解析以获取输出文件


相反,如果我可以保持“execute”可执行文件处于打开状态,并且仍然执行python exec.py variables.in.1 results.out.1,脚本将解析输入文件,然后将其发送到打开的可执行文件,然后创建输出,那么我的工作将更快、更高效

是否有一种方法可以增加
exec.py
以获取任意数量的输入/输出文件名对

如果是,调用将如下所示:

python exec.py variables.in.1 output.out.1 ... variables.in.N output.out.N python exec.py variables.in.1 output.out.1。。。变量.in.N输出.out.N 现在,假设你有1000双这样的鞋。您可以将此范围划分为20组,每组50对(例如)。并使用不同的组(同时)调用脚本20次:

python exec.py variables.in.1 output.out.1 ... variables.in.50 output.out.50 python exec.py variables.in.51 output.out.51 ... variables.in.100 output.out.100 ... python exec.py variables.in.951 output.out.951 ... variables.in.1000 output.out.1000 python exec.py variables.in.1 output.out.1。。。变量.in.50输出.out.50 python exec.py variables.in.51 output.out.51。。。变量.in.100输出.out.100 ... python exec.py variables.in.951 output.out.951。。。变量.in.1000输出.out.1000
是否有一种方法可以增加
exec.py
以获取任意数量的输入/输出文件名对

如果是,调用将如下所示:

python exec.py variables.in.1 output.out.1 ... variables.in.N output.out.N python exec.py variables.in.1 output.out.1。。。变量.in.N输出.out.N 现在,假设你有1000双这样的鞋。您可以将此范围划分为20组,每组50对(例如)。并使用不同的组(同时)调用脚本20次:

python exec.py variables.in.1 output.out.1 ... variables.in.50 output.out.50 python exec.py variables.in.51 output.out.51 ... variables.in.100 output.out.100 ... python exec.py variables.in.951 output.out.951 ... variables.in.1000 output.out.1000 python exec.py variables.in.1 output.out.1。。。变量.in.50输出.out.50 python exec.py variables.in.51 output.out.51。。。变量.in.100输出.out.100 ... python exec.py variables.in.951 output.out.951。。。变量.in.1000输出.out.1000 你想要的是

1) 创建始终运行的守护进程

2) 提供受监视文件或文件夹的列表作为参数

3) 使用inotify检测新传入的文件

4) 每次inotify发出信号时,通过调用方法触发守护进程内的转换

如果语法中有“python”命令,则不能保持语法不变,因为每次都会重新启动解释器

1) 创建始终运行的守护进程

2) 提供受监视文件或文件夹的列表作为参数

3) 使用inotify检测新传入的文件

4) 每次inotify发出信号时,通过调用方法触发守护进程内的转换


如果语法中有“python”命令,则无法保持语法不变,因为这自然会每次重新启动解释器。

这里需要的是“subprocess”模块,它使启动进程并连接其输入和输出管道变得简单

为了避免阻塞,只需在单独的进程中执行输入和输出。您可以通过“os.fork”实现这一点:

导入操作系统
导入子流程
execute=subprocess.Popen(
“执行”,
stdin=子流程.PIPE,
stdout=子流程.PIPE
)
如果os.fork():
#将数据输入到输入中
尽管如此:
输入=。。。
执行.stdin.write(输入)
其他:
输出生成器=输出
#对输出做些什么
对于execute.stdout中的大纲:
打印大纲

或者,您可以将输入和输出代码拆分为两个文件,然后使用类似以下内容作为“main”:

导入操作系统
导入子流程
generateInput=subprocess.Popen(
['python','generateInput.py'],
stdout=子流程.PIPE
)
execute=subprocess.Popen(
“执行”,
stdin=generateInput.stdout,
stdout=子流程.PIPE
)
generateOutput=subprocess.Popen(
['python','generateOutput.py'],
stdin=execute.stdout
)
execute.communicate()

这只是一个很长的说法


python generateInput.py | execute | python gnerateOutput.py

这里需要的是“subprocess”模块,它使启动进程并连接其输入和输出管道变得简单

为了避免阻塞,只需在单独的进程中执行输入和输出。您可以通过“os.fork”实现这一点:

导入操作系统
导入子流程
execute=subprocess.Popen(
“执行”,
stdin=子流程.PIPE,
stdout=子流程.PIPE
)
如果os.fork():
#将数据输入到输入中
尽管如此:
输入=。。。
执行.stdin.write(inpu
python generateInput.py | execute | python gnerateOutput.py