简体   繁体   English

进程间通信:命名管道或状态文件

[英]Inter-process communication: named pipes or status files

I'm building a tool that consists of a Webservice that will run commandline tools at specific times. 我正在构建一个包含Web服务的工具,该工具将在特定时间运行命令行工具。 On average there will be running 15-20 CL tools at the same time. 平均而言,将同时运行15-20个CL工具。 Every CL tool runs no longer than about 0,5-1 minute on average. 每个CL工具平均运行时间不超过约0.5-1分钟。 The webservice needs to be able to check each CL's status every 2-3 seconds or so. Web服务需要能够每2-3秒左右检查一次每个CL的状态。

I've got some advice about how to do this; 我对如何做有一些建议。 named pipes looks likes the best "technical" solution. 命名管道看起来像最好的“技术”解决方案。 However I'm wondering if communicating with simple very small text "status" files would be a better, less complicated and less error-risk, way. 但是,我想知道与简单的非常小的文本“状态”文件进行通信是否会是一种更好,更简单且出错风险更低的方法。 I prefer the solution that is the most resource friendly. 我喜欢最资源友好的解决方案。

Please advice. 请指教。

I did something similar before and here's what I did. 我之前做过类似的事情,这就是我所做的。

I start the command line process by setting ProcessStartInfo.RedirectStandardOutput to true . 我通过将ProcessStartInfo.RedirectStandardOutput设置为true来启动命令行过程。 Then I start listening to the command line process' output by doing: 然后,通过执行以下操作开始监听命令行进程的输出:

process.OutputDataReceived += dataReceivedEventHandler;
process.BeginOutputReadLine();

Then in my dataReceivedEventHandler , I parse DataReceivedEventArgs.Data to see if my console application sent me a "message" that begins with a tag that I created (ex: "[ServerProgress]10"). 然后在我的dataReceivedEventHandler ,解析DataReceivedEventArgs.Data以查看我的控制台应用程序是否向我发送了一个以我创建的标签开头的“消息”(例如:“ [ServerProgress] 10”)。

So basically, the applications are communicating through the console output. 因此,基本上,应用程序是通过控制台输出进行通信的。 Not elegant, but it did the job for me. 不优雅,但是它为我做了工作。

Hope this helps. 希望这可以帮助。

For your situation, status checks - not a huge footprint there, I'd go with named pipes any day. 对于您的情况,状态检查-在那里不占很大空间,我每天都会使用命名管道。 Using memory >> using HDD here. 使用内存>>在此处使用HDD。

Named pipes would be better. 命名管道会更好。 You might run into access problems with the two processes trying to access the small text file at once. 您可能会遇到尝试同时访问小文本文件的两个过程的访问问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM