我正在尝试使用Inkscape的命令行功能在Java中创建一个前端应用程序来处理批量SVG转换.我正在从https://sourceforge.net/projects/conversionsvg/获取并更新代码.原始开发人员通过Runtime.getRuntime().exec(String)处理Inkscape的方式.我遇到的问题是使用methodA和methodB之间存在一些不一致.我创建了一个简单的java测试项目来演示正在执行的不同操作.
CallerTest.java
package conversion;
import java.io.IOException;
public class CallerTest {
static String pathToInkscape = "\"C:\\Program Files\\Inkscape\\inkscape.exe\"";
public static void main(String[] args) {
ProcessBuilderCaller processBuilder = new ProcessBuilderCaller();
RuntimeExecCaller runtimeExec = new RuntimeExecCaller();
// methodA() uses one long command line string
try {
String oneLongString_ProcessBuilder = pathToInkscape + " -f \"C:\\test.svg\" -D -w 100 -h 100 -e \"C:\\ProcessBuilder-methodB.png\"";
String oneLongString_RuntimeExec = pathToInkscape + " -f \"C:\\test.svg\" -D -w 100 -h 100 -e \"C:\\RuntimeExec-methodA.png\"";
// processBuilder.methodA(oneLongString_ProcessBuilder);
runtimeExec.methodA(oneLongString_RuntimeExec);
} …Run Code Online (Sandbox Code Playgroud) 可能重复:
具有超时的子进程
在Python中执行以下操作的最简单方法是什么:
我想要这样的东西:
import proc
try:
status, stdout, stderr = proc.run(["ls", "-l"], timeout=10)
except proc.Timeout:
print "failed"
Run Code Online (Sandbox Code Playgroud) 我有一个在我的Web服务器上运行的python脚本.然后调用main函数,当它返回时,它只是休眠几秒钟并再次被调用.它的目的是获取用户添加的任何新上传的视频并将其转换为webm,将中间帧作为图像和一堆其他时髦的东西拉出来.我正在使用外部调用ffmpeg.下面的代码片段显示了我如何调用它.
duration = output[durationIndex+10:durationIndex+18]
durationBits = duration.split(":")
lengthInSeconds = (int(durationBits[0])*60*60) + (int(durationBits[1])*60) + (int(durationBits[2]))
child = subprocess.Popen(["ffmpeg","-y","-i",sourceVideo,"-f","mjpeg","-vframes","1","-ss",str(lengthInSeconds/2),destination], shell=True, stderr=subprocess.PIPE)
output = ""
while True:
out = child.stderr.read(1)
if out == '' and child.poll() != None:
break
if out != '':
output += out
updateSQL = "update `videos_graduatevideo` set thumbnail = '" + str(destination) + "' where `original_video` = '" + sourceVideo + "'"
cursor.execute(updateSQL)
Run Code Online (Sandbox Code Playgroud)
这个脚本在Windows机器上运行,但我可能会在开发完成时将它部署在Unix系统上.
问题是.我需要这个python脚本来继续运行.如果ffmpeg出现问题并且我的脚本挂起,用户上传的视频将处于"挂起"状态,直到我开始查看python脚本.我知道某个mov文件让ffmpeg无限期挂起.在某种程度上,我可以检查一个进程运行了多长时间,然后如果它运行了太长时间就将其终止?
我在.NET 4.0中运行的C#Web应用程序中使用WkhtmltoPdf从HTML文件生成PDF.通常一切正常,除非HTML文件的大小低于250KB.一旦HTML文件大小超过该值,运行wkhtmltopdf.exe的进程就会产生如下异常.在任务管理器上,我已经看到wkhtmltopdf.exe进程的内存值没有超过40,096 K的值,我相信这就是为什么在这两者之间放弃进程的原因.
我们如何配置外部exes的内存限制可以增加?有没有其他方法可以解决这个问题?
更多信息:
当我直接从命令行运行转换时,PDF生成正常.所以,它不太可能成为WkhtmlToPdf的问题.
错误来自localhost.我在DEV服务器上尝试过相同的结果.
编辑:
更具体的异常消息: - 对于Process对象的MainModule属性,错误显示 - {"只有ReadProcessMemory或WriteProcessMemory请求的一部分已完成"},具有NativeErrorCode值 - 299.
例外:
> [Exception: Loading pages (1/6) [>
> ] 0% [======> ]
> 10% [======> ] 11%
> [=======> ] 13%
> [=========> ] 15%
> [==========> ] 18%
> [============> ] 20%
> [=============> ] 22%
> [==============> ] 24%
> [===============> ] 26%
> [=================> ] 29%
> [==================> ] 31%
> [===================> ] 33%
> [=====================> ] 35%
> [======================> ] 37% …Run Code Online (Sandbox Code Playgroud) 我们的 Intranet 中有一个自定义 Web 应用程序,允许用户以更适合我们组织的方式浏览和搜索我们的共享文件系统。与 windows 资源管理器/mac finder 相比。但是,当用户单击例如指向 word 文档的链接时,该文档会被浏览器下载然后打开。我试图提供一种更好的方法,即直接从每个用户在他自己的计算机中映射的共享文件夹中打开文件。这将使事情变得更快,并且不会污染浏览器下载文件夹。
我计划创建一个 chrome 或 firefox 扩展,它识别附加到链接的某些 css 类,将链接重新映射到共享文件系统并启动一个外部进程。知道如何实现这一目标吗?有更好的解决方案吗?
在我的 Lua 程序中,我必须捕获外部程序的输出。该外部程序需要某些环境变量。所以我这样做:
e = ""
e = e .. "A=100;"
e = e .. "B=Hi;"
e = e .. "C=Test;"
file = io.popen(e .. "/bin/aprogr")
Run Code Online (Sandbox Code Playgroud)
显然,如果环境很大,popen() 的参数可能会超过限制(如果有)。
还有其他方法将环境传递给外部程序吗?
为什么在下面的 Julia 代码中,并行实现比串行运行慢?
using Distributed
@everywhere function ext(i::Int64)
callmop = `awk '{ sum += $1 } END { print sum }' infile_$(i)`
run(callmop)
end
function fpar()
@sync @distributed for i = 1:10
ext(i)
end
end
function fnopar()
for i = 1:10
ext(i)
end
end
val, t_par, bytes, gctime, memallocs = @timed fpar()
val, t_nopar, bytes, gctime, memallocs = @timed fnopar()
println("Parallel: $(t_par) s. Serial: $(t_nopar) s")
# Parallel: 0.448290379 s. Serial: 0.028704802 s
Run Code Online (Sandbox Code Playgroud)
这些文件infile_$(i)包含一列实数。经过一些研究,我遇到了处理类似问题的这篇文章和另一篇文章)。不过,如果考虑到 …
我想编写一个运行外部"java myprog <input.txt> output.txt"命令的Java程序.最终目标是在两个不同的程序上运行此命令,并将它们的输出相似性与各自的输出文件进行比较.
我想我已经阅读了关于使用ProcessBuilder运行外部程序的所有相关文章,以及关于在该外部程序中处理用户输入的一些条目,但我仍然无法使其工作.根据我的阅读,我认为最好的方法是不运行上面的确切命令,而是读取input.txt文件并将其逐字节地提供给Process对象,然后收集输出并将其写入输出.txt ...我对其他选项100%开放.
我根据读数将下面的代码放在一起.它似乎正确地将input.txt中的输入提供给myprog,但是当我尝试将外部程序的输出打印到控制台进行验证时,程序会在myprog中预期(惊讶)用户输入的位置挂起.
无论有没有redirectErrorStream(true)行,我都会遇到同样的问题.
我真的希望这是Java,因为我打算与我将比较的程序输出的人共享源代码,他们主要只熟悉Java.
import java.io.*;
import java.util.*;
public class test7 {
public static void main(String args[]) {
try {
// WANT: "java myprog < input.txt > output.txt"
String inputFile = "input.txt";
String outputFile = "output.txt";
ProcessBuilder pb = new ProcessBuilder("java","myprog");
pb.redirectErrorStream(true); // merge stdout, stderr of process
Process p = pb.start();
// write input to the running program
OutputStream pos = p.getOutputStream();
InputStream fis = new FileInputStream(inputFile);
int read = 0;
while ( (read …Run Code Online (Sandbox Code Playgroud) 我已经阅读了subprocess.Popen上的所有内容,但是我想我缺少了一些东西。
我需要能够执行一个Unix程序,该程序从python脚本中创建的列表中读取数据流并将该程序的结果写入文件。在bash提示符下,我一直都没问题,但现在我尝试从python脚本中执行此操作,该脚本在进入此阶段之前会先预处理一些二进制文件和大量数据。
让我们看一个不包含所有预处理的简单示例:
import sys
from pylab import *
from subprocess import *
from shlex import split
# some arbitrary x,y points
points = [(11,31),(13,33),(15,37),(16,35),(17,38),(18,39.55)]
commandline = 'my_unix_prog option1 option2 .... > outfile'
command = split(commandline)
process = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
print process.communicate(str(points))
Run Code Online (Sandbox Code Playgroud)
在bash中执行的方式是:
echo "11 31
13 33
15 37
16 35
17 38
18 39.55" | my_unix_prog option1 option2 .... > outfile
Run Code Online (Sandbox Code Playgroud)
数据输入到unix prog的方式也很重要,我应该格式化为两列,并用空格隔开。
任何帮助表示赞赏...
因此,我设置了一个执行外部程序(用 Fortran 77 编写)的简短脚本。我想运行该程序的多个实例,由于我的计算机上有 8 个内核,我发现的最简单的解决方案是:
import subprocess
import os
i = n
while(i<n):
dire = "dir/Run"+str(i)+"/"
os.chdir(dire)
p1 = subprocess.Popen(['./mej'])
dire = "dir/Run"+str(i+1)+"/"
os.chdir(dire)
p2 = subprocess.Popen(['./mej'])
dire = "dir/Run"+str(i+2)+"/"
os.chdir(dire)
p3 = subprocess.Popen(['./mej'])
dire = "dir/Run"+str(i+3)+"/"
os.chdir(dire)
p4 = subprocess.Popen(['./mej'])
dire = "dir/Run"+str(i+4)+"/"
os.chdir(dire)
p5 = subprocess.Popen(['./mej'])
dire = "dir/Run"+str(i+5)+"/"
os.chdir(dire)
p6 = subprocess.Popen(['./mej'])
dire = "dir/Run"+str(i+6)+"/"
os.chdir(dire)
p7 = subprocess.Popen(['./mej'])
dire = "dir/Run"+str(i+7)+"/"
os.chdir(dire)
p8 = subprocess.Popen(['./mej'])
dire = "/Run"+str(i+8)+"/"
os.chdir(dire)
p3 = subprocess.Popen(['./mej'])
exit_codes = [p.wait() …Run Code Online (Sandbox Code Playgroud) external-process ×10
python ×4
java ×2
subprocess ×2
asp.net ×1
browser ×1
inkscape ×1
julia ×1
lua ×1
performance ×1
piping ×1
process ×1
runtime.exec ×1
user-input ×1
web ×1
wkhtmltopdf ×1