【发布时间】:2014-03-06 08:11:21
【问题描述】:
对于 perl 专家来说,这可能是一个简单的问题 -
我正在尝试运行一个在逻辑上应该执行此操作的脚本>
Main
{
While (true)
Call Sub A
Call Sub B
Call Sub C
}
Sub A
{
Go execute a method A for whatever it needs to do ---
before next time method A runs it need to wait atleast 60 seconds but i still want to call method B in these 60 seconds.
}
Sub B
{
Go execute a method B for whatever it needs to do ---
before next time method b runs it need to wait atleast 60 seconds but i still want to call method C in these 60 seconds in mean time.
}
Sub C
{
Go execute a method C for whatever it needs to do ---
before next time method C runs it need to wait atleast 60 seconds at this moment control should be back in Main and wait for first 60 seconds of A to expire so that Sub A can be called
}
我的问题: 问:我能做到这一点的最佳和优化方式是什么?
问:如果我在每个 sub 中设置 sleep 60,那么即使在 60 秒到期之前,下一个 sub 也不会被调用,这将延迟整体处理。
问:我希望每 60 秒按顺序调用 3 个潜艇
Q 最后,如果我需要每 60 秒呼叫 2 个 subs 并且每小时最后一个 subs - 我该怎么做?
评论 - 我的想法是将 UTC 作为一个变量并将其存储在一个变量中,并在时间到期时继续检查时间,而不是调用单个潜艇,但不确定它是否是运行代码的最佳方式。
【问题讨论】:
-
每个子程序都需要几秒钟吗?总和总是小于 60 秒吗?如果 A 在 60 秒内完成但 B 还没有完成,是否应该再次运行 A? C呢?你可以使用多个线程,所以有一个 A 线程以不高于每 60 秒一次的频率运行 A,一个 B 线程用于 B,一个 C 线程用于 C?
-
使用线程可能是这里的方式,但请记住,如果您的 subs 与公共全局变量混合(全局不仅在相应的 sub 中),您必须确保没有两个线程试图同时修改同一个变量。这不是一项简单的任务。
-
所以理论上没有一个应该花费超过几秒钟(这些是 SOAP 调用),我的测试表明它可能需要很少的几秒钟,并且绝对总和不会等于或大于 60 秒。但是我想再次采取最佳实践,而不是依赖我的想法.....所以我可以使用多线程程序(我不知道怎么做?)如果可能的话,我需要一些伪代码帮助在 perl 中。并解释它是如何工作的......感谢并感谢您的回复。
-
perldoc.perl.org/perlthrtut.html 如果你不明白里面的内容,问。
-
你想在 A 结束之前给 B 打电话吗?线程的答案是假设的,但在我看来,您希望它们是连续的,这意味着您不想想要使用线程。
标签: perl