使用什么 CPAN 模块通过 Perl 进行非阻塞文件锁定

发布于 2024-10-03 22:50:33 字数 392 浏览 1 评论 0原文

我想确保一次只运行一个进程。所以我想确保 jobB 不会运行,除非 jobA 没有运行。

如果它能够重试锁定,那就太好了,这样我就不必编写代码了。大概是这样的:

LockFileModule->lock(
    lockfile           => '/fabulous/pants',
    retries            => 12,
    timeout            => 25,
    timebetweenretries => 30,
) or die "the other job is still running";

在 Perl 中执行此操作的最佳方法是什么?我希望有一个很好的 CPAN 模块来解决这个问题。

I want to make sure only one process at a time runs. So I want to make sure jobB doesn't run unless jobA is not running.

It would be great if it had some ability to retry the lock so I don't have to code that. Something vaguely like this:

LockFileModule->lock(
    lockfile           => '/fabulous/pants',
    retries            => 12,
    timeout            => 25,
    timebetweenretries => 30,
) or die "the other job is still running";

Whats the best way to do this in Perl? I'm hoping there is a good CPAN module for this.

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

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

发布评论

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

评论(1

意中人 2024-10-10 22:50:33
#!/usr/bin/perl
unless (flock(DATA, LOCK_EX|LOCK_NB)) {
    $logger->info("$0 is already running. Exiting.\n" );
    exit(1);
} else {
    $logger->info("$0 not already running, so starting instance now." );
}

__DATA__
Do not delete this. Used for flock code above

这将锁定程序本身的DATA部分。我使用这种技术,效果非常好。

您可以扩展它以非常轻松地启用重试。

#!/usr/bin/perl
unless (flock(DATA, LOCK_EX|LOCK_NB)) {
    $logger->info("$0 is already running. Exiting.\n" );
    exit(1);
} else {
    $logger->info("$0 not already running, so starting instance now." );
}

__DATA__
Do not delete this. Used for flock code above

This will lock the DATA section of the program itself. I use this technique, and it works very well.

You can expand it to enable retries pretty easily.

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