Perl IPC - FIFO 和守护进程 &中央处理器使用率

发布于 2024-10-13 11:15:18 字数 800 浏览 5 评论 0原文

我有一个邮件解析器 perl 脚本,每次用户收到邮件时都会调用该脚本(使用 .qmail)。它从邮件中提取日历附件,并将文件的“路径”放入使用 目录::队列模块

另一个 perl 脚本读取日历附件的路径并在本地系统以及远程 CalDAV 服务器上执行某些文件操作,它作为守护程序运行,如所解释的 此处。所以基本上这个脚本看起来像:

my $declarations

sub foo {
.
.
}

sub bar {
. 
. 
}

while ($keep_running) {
    for(keep-checking-the-queue-for-new-entries) {

        sub caldav_logic1 {
        .
        .
     }
        sub caldav_logic2 {
        .
        . 
    }
  }
}  

我使用 Proc::Daemon 将脚本作为守护进程运行。现在的问题是,这个进程的CPU使用率几乎是100%。以更标准、更安全的方式实现守护进程的建议方法是什么?我使用的代码与 Proc::Daemon 的使用提到的链接中提到的代码几乎相同。

I have a mail parser perl script which is called every time a mail arrives for a user (using .qmail). It extracts a calendar attachment out of the mail and places the "path" of the file in a FIFO queue implemented using the Directory::Queue module.

Another perl script which reads the path of the calendar attachment and performs certain file operations on the local system as well as on the remote CalDAV server, is being run as a daemon, as explained here. So basically this script looks like:

my $declarations

sub foo {
.
.
}

sub bar {
. 
. 
}

while ($keep_running) {
    for(keep-checking-the-queue-for-new-entries) {

        sub caldav_logic1 {
        .
        .
     }
        sub caldav_logic2 {
        .
        . 
    }
  }
}  

I am using Proc::Daemon for running the script as a daemon. Now the problem is, this process has almost 100% CPU usage. What are the suggested ways to implement the daemon in a more standard, safer way ? I am using pretty much the same code as mentioned in the link mentioned for usage of Proc::Daemon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-10-20 11:15:18

我敢打赌这是你的 for 循环并检查新的队列条目。

有多种方法可以监视目录中的文件更改。这些方式取决于操作系统,但可能有一个 Perl 模块为您包装它们。使用它而不是忙循环。即使有睡眠延迟,当您可以让程序准确地被操作系统事件唤醒时,循环效率也很低。

File::ChangeNotify 看起来很有希望。

I bet it is your for loop and checking for new queue entries.

There are ways to watch a directory for file changes. These ways are OS dependent but there might be a Perl module that wraps them up for you. Use that instead of busy looping. Even with a sleep delay, the looping is inefficient when you can have your program told exactly when to wake up by an OS event.

File::ChangeNotify looks promising.

预谋 2024-10-20 11:15:18

也许您不想要真正的连续轮询。即使队列为空,keep-checking-the-queue-for-new-entries 是否是代码中 CPU 密集型部分?这可以解释为什么你的处理器总是忙碌。

尝试将 sleep 1 语句放在 while 循环的最顶部(或最底部),让处理器在队列检查之间休息。如果这不会使程序性能降低太多(即,如果每个人都可以容忍在公司日历更新之前多等待一秒钟),并且如果 CPU 使用率看起来仍然很高,请尝试 sleep 2,<代码>睡眠5等

Maybe you don't want truly continuous polling. Is keep-checking-the-queue-for-new-entries a CPU-intensive part of the code, even when the queue is empty? That would explain why your processor is always busy.

Try putting a sleep 1 statement at the very top (or very bottom) of the while loop to let the processor rest between queue checks. If that doesn't degrade the program performance too much (i.e., if everyone can tolerate waiting an extra second before the company calendars get updated) and if the CPU usage still seems high, try sleep 2, sleep 5, etc.

瀞厅☆埖开 2024-10-20 11:15:18
cpan Linux::Inotify2

内核知道文件何时更改并将此信息发送到运行子程序的程序。也许这会更好,因为程序仅在文件更改时才运行子程序。

cpan Linux::Inotify2

The kernel knows when files change and sends this information to your program which runs the sub. Maybe this will be better because the program will run the sub only when the file is changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文