我正在开发一个小型Java应用程序,该应用程序需要启动python脚本并与之交互。 python脚本将在后台运行并等待命令。在执行每个命令之后,我希望得到一个响应,该响应将转发回Java应用程序。
我的问题是,如何在不重新运行python脚本钩子的情况下运行它并运行命令?
public void startProcess()
{
try {
p = Runtime.getRuntime().exec("python " + scriptPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public String executeCommand(String cmd)
{
String consoleResponse = "";
try {
// how do I perform something similar to p.exec(cmd)
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((consoleResponse += stdInput.readLine()) != null) {
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((consoleResponse = stdError.readLine()) != null) {
}
} catch (IOException e) {
e.printStackTrace();
}
return consoleResponse;
}
编辑:python脚本用于BACpypes。该脚本执行3件事。 WhoIs:获取通过bacnet连接的所有设备的列表 ReadHexFile:读取文本文件以发送到网络上的所有设备 SendFile:将文件发送到所有设备。
我对python没有经验,因此将所有这些数据保存在一个脚本中会更简单。
我想一个选择是将每个命令分解成自己的脚本,然后将数据传递给Java应用程序。
答案 0 :(得分:0)
我如何在不重新运行python脚本钩子的情况下运行命令?
您需要使单个Python脚本继续监听新的输入或请求(来回通信),但是我认为这会有些麻烦,并且会使您的python脚本不如标准{{1 }}流。
避免运行多个Python脚本的原因是什么?
要将输入内容写入脚本标准输入,请执行以下操作:
using Ipopt
using ForwardDiff
using ForwardDiff: JacobianConfig`
# min tf = X[N+1] (where X = [U ; tf] and U = [u1 u2 ... uN])
# st x1(t) = x2(t)
# x2(t) = du/dt
# x1(0) = -1 ; x2(0) = 0
# x1(tf) = 0 ; x2(tf) = 0
# Start at (-1,0)
x10 = -1.0
x20 = 0.0
# End at (0,0)
x1f = 0.0
x2f = 0.0
N = 20
uinit = zeros(N,1)
tfinit = 1.0
xinit = [uinit ; tfinit]
x_L = -ones(N+1,1)
x_L[N+1] = 0.0
x_U = ones(N+1,1)
x_U[N+1] = 20.0
# Number of variables
n = N+1
# Number of constraints
m = 2
g_L = [-2.0e19, -2.0e19]
g_U = [2.0e19, 2.0e19]
f1(x1,x2,u) = x2
f2(x1,x2,u) = u
### Constructor for the jacobian ###
# Using autodiff
function construct_sjac(sfun!::Function)
function sjac!(j::AbstractArray{Float64,2}, y::AbstractArray{Float64,1})
function mysfun!(s_, y_)
sfun!(s_, y_)
end
s = fill!(similar(y), 0.0)
ForwardDiff.jacobian!(j, mysfun!, s, y, JacobianConfig(mysfun!, s, y), Val{true}())
end
return sjac!
end
### The function to minimize ###
function eval_f(x)
return x[N+1]
end
### The constraint function ###
function eval_g!(x, g)
N = size(x)[1]
# Bad: g = zeros(2) # Allocates new array
# OK: g[:] = zeros(2) # Modifies 'in place'
g[1] = x10
g[2] = x20
for i=1:N
g[1] = g[1] + tf/N * f1(g[1],g[2],x[i])
g[2] = g[2] + tf/N * f2(g[1],g[2],x[i])
end
end
### The gradient of the function to minimize ###
function eval_grad_f(x, grad_f)
grad_f[:] = zeros(N+1)
grad_f[N+1] = 1
end
### The jacobian of the constraint function ###
function eval_jac_g(x, mode, rows, cols, values)
if mode == :Structure
# Constraint (row) 1
rows[1] = 1; cols[1] = 1
rows[2] = 1; cols[2] = 2
# Constraint (row) 2
rows[3] = 2; cols[3] = 1
rows[4] = 2; cols[4] = 2
else
N = size(x)[1]
sjac! = construct_sjac(eval_g!)
for i=1:N
# Constraint (row) 1
values[i] = sjac![1][i] #1,i
# Constraint (row) 2
values[N+i] = sjac![2][i] #2,i
end
end
end
# We generate the problem to solve #
prob = createProblem(n, x_L, x_U, m, g_L, g_U, 2*N, 2*N,
eval_f, eval_g!, eval_grad_f, eval_jac_g, eval_h = nothing)
# Set starting solution #
prob.x = xinit
# Solve #
status = solveProblem(prob)
println(Ipopt.ApplicationReturnStatus[status])
println(prob.x)
println(prob.obj_val)