在 Perl 中分叉多个子项并使用管道进行双向通信
我正在尝试创建一个具有多种处理功能的小型 Perl 程序。由于我的要求中存在一些小的变化,因此我无法在任何地方找到任何类似的示例脚本。
我需要从 STDIN 读取一个大日志文件,并将前 N 行(又是一个大数字)提供给第一个子进程,然后将接下来的 N 行提供给第二个子进程等。我还定义了一个常量,它是允许同时运行的最大子进程数。一旦子进程达到最大数量,父进程将等待子进程完成其工作并再给它 N 行。
父进程还收集每个子进程完成时返回的多行(5-10 行)输出并将其存储在数组中。然后Parent继续处理这个数组内容并最终显示结果。
是否有更好的示例脚本可供我修改和使用,或者有人可以通过在此处分享一个来帮助我吗?我更喜欢仅使用管道进行进程间通信,并尽可能让事情变得简单。
编辑: 有人可以举例说明如何仅使用 IO::Handle 模块中的管道来完成此操作吗?
I am trying to create a small Perl program which has multi processing capabilities. Since there are some small changes here and there in my requirements, I am not able to find any similar sample scripts anywhere.
I need to read a big logfile from STDIN and give first N number (a big number again) of lines to the first child process and then next N number of lines to the second child process etc. I have also a constant defined which is the maximum number of child processes allowed to run concurrently. Once maximum number of children reached, parent will wait for a child to finish its job and give another N number of lines to it.
The parent process also collects a multi-line (5-10 lines) output returned by each child process when they finish and stores it in an array. Parent then continues to process this array contents and display the results finally.
Is there a better sample script which I can modify and use or could someone help me by sharing one here? I prefer using only pipes for process intercommunication and keep things simpler as much as possible.
Edit:
Can someone show an example how this can be accomplished only using pipes from IO::Handle module ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Forks::Super
,这很容易限制同时进程的数量并处理进程间通信。例如,Use
Forks::Super
, which makes it easy to throttle the number of simultaneous processes and handle the interprocess communication. For example,我发现线程对于此类过程要简单得多。您需要线程和 Threads::Queue 模块。该过程是建立一个队列来为工作线程提供数据,并建立一个队列来让它们返回结果。工作线程只是读取记录、处理记录并将结果发回的函数。我只是将这段代码放在一起并没有对其进行测试,因此它可能有错误,但我认为显示了总体思路:
I have found threads to be much simpler for this sort of process. You need the threads and Threads::Queue modules. The process is to set up a queue to feed the worker threads and one for them to return their results. The worker thread is just a function to read a record, process it and send a result back. I just put this code together and have not tested it so it may be buggy but I think shows the general idea: