gpt4 book ai didi

c# - 运行太多进程时 Mono 崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 09:04:02 24 4
gpt4 key购买 nike

我想建立一个视频转换队列,所以服务器运行一个单声道程序,从队列中提取一个视频并运行 ffmpeg 来转换它。当运行许多 ffmpeg 进程时,单声道给我错误。

所以我用 ping 创建了一个测试用例,因为它是一个像 ffmpeg 一样长时间运行的测试用例。

    static TextWriter tw = new StreamWriter("error.txt");
private static int _started;
public static void Main (string[] args)
{

for (int i=0; i<1000; i++) {
Start ("ping", "localhost");

}
Console.WriteLine("Memory: " + (GC.GetTotalMemory(true) / 1024) + "KB");
Console.WriteLine(_started);
tw.Flush();
Console.ReadLine();
}

public static Process Start (string name, string args)
{
try {
var proc = new ProcessStartInfo {
FileName = name,
Arguments = args,
RedirectStandardOutput = true,
CreateNoWindow = false,
RedirectStandardError = false,
UseShellExecute = false,
RedirectStandardInput = false,
};
Process.Start (proc);
_started++;
} catch (Exception ex) {
tw.WriteLine (ex.InnerException+Environment.NewLine+ex.Message + Environment.NewLine + ex.StackTrace, false);
tw.Flush();
}
return null;
}

运行时,会抛出与 ffmpeg 相同的错误

ApplicationName='ping', CommandLine='localhost', CurrentDirectory=''
at System.Diagnostics.Process.Start_noshell
(System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process
process) [0x00000] in <filename unknown>:0
at System.Diagnostics.Process.Start_common
(System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process
process) [0x00000] in <filename unknown>:0
at System.Diagnostics.Process.Start (System.Diagnostics.ProcessStartInfo
startInfo) [0x00000] in <filename unknown>:0
at test1.MainClass.Start (System.String name, System.String args) [0x00000]
in <filename unknown>:0

CreateProcess: error creating process handle

这就是我的环境:

Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-5ubuntu1)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: amd64
Disabled: none
Misc: softdebug
LLVM: supported, not enabled.
GC: Included Boehm (with typed GC and Parallel Mark)

和极限值

core file size          (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 31439
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 31439
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

我想继续我的项目,但因为这个异常我不能,在使用 mono 工作 1 年后我真的很喜欢它,并且因为这个麻烦不想学习其他语言。

我希望我的代码有问题,而不是单声道运行时。

所以我的临时解决方案:

    public static void Start (string cmd, int time)
{
new Thread (p =>
{
try {
Mono.Unix.Native.Syscall.system (String.Format("timeout {0} {1}", time, cmd));
} catch (Exception ex) {
//log it
}
}
).Start ();
}

最佳答案

您必须在 Process 实例上调用 WaitForExit 和 Dispose。

所以您的 Start 方法看起来像这样:

public static void Start (string name, string args)
{
new Thread (() =>
{
try {
var proc = new ProcessStartInfo {
FileName = name,
Arguments = args,
RedirectStandardOutput = true,
CreateNoWindow = false,
RedirectStandardError = false,
UseShellExecute = false,
RedirectStandardInput = false,
};
using (var p = new Process ()) {
p.StartInfo = proc;
p.Start ();
p.WaitForExit ();
}
_started++;
} catch (Exception ex) {
tw.WriteLine (ex.InnerException+Environment.NewLine+ex.Message + Environment.NewLine + ex.StackTrace, false);
tw.Flush();
}
}).Start ();
}

关于c# - 运行太多进程时 Mono 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14982740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com