multithreading perl 代码的多线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15429183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:21:43  来源:igfitidea点击:

Multithreading for perl code

multithreadingperl

提问by NewBee

I need to know how to implement multi threading for the following code . I need to call this script every second but the sleep timer processes it after 2 seconds . In total script call itself after every 3 seconds. But i need to call it every second , can anybody provide me a solution to it or point me to right direction.

我需要知道如何为以下代码实现多线程。我需要每秒调用此脚本,但睡眠计时器会在 2 秒后处理它。总共脚本每 3 秒调用一次。但是我需要每秒调用它,有人可以为我提供解决方案或为我指明正确的方向。

#!usr/bin/perl
use warnings;

sub print
{
local $gg = time;
print "$gg\n";
}

$oldtime = (time + 1);
while(1)
{
if(time > $oldtime)
{
    &print();
    sleep 2;
    $oldtime = (time + 1);
            }
        }

Its just an example.

它只是一个例子。

回答by

Here is a simple example of using threads:

下面是一个使用线程的简单示例:

use strict;
use warnings;
use threads;

sub threaded_task {
    threads->create(sub { 
        my $thr_id = threads->self->tid;
        print "Starting thread $thr_id\n";
        sleep 2; 
        print "Ending thread $thr_id\n";
        threads->detach(); #End thread.
    });
}

while (1)
{
    threaded_task();
    sleep 1;
}

This will create a thread every second. The thread itself lasts two seconds.

这将每秒创建一个线程。线程本身持续两秒钟。

To learn more about threads, please see the documentation. An important consideration is that variables are not shared between threads. Duplicate copies of all your variables are made when you start a new thread.

要了解有关线程的更多信息,请参阅文档。一个重要的考虑是变量在线程之间不共享。当您启动一个新线程时,所有变量都会被复制。

If you need shared variables, look into threads::shared.

如果您需要共享变量,请查看threads::shared.

However, please note that the correct design depends on what you are actually trying to do. Which isn't clear from your question.

但是,请注意,正确的设计取决于您实际尝试执行的操作。从你的问题中不清楚。

Some other comments on your code:

关于您的代码的其他一些评论:

  • Always use strict;to help you use best practices in your code.
  • The correct way to declare a lexical variable is my $gg;rather than local $gg;. localdoesn't actually create a lexical variable; it gives a localized value to a global variable. It is not something you will need to use very often.
  • Avoid giving subroutines the same name as system functions (e.g. print). This is confusing.
  • It is not recommended to use &before calling subroutines (in your case it was necessary because of conflict with a system function name, but as I said, that should be avoided).
  • 始终use strict;帮助您在代码中使用最佳实践。
  • 声明词法变量的正确方法是my $gg;而不是local $gg;. local实际上并不创建词法变量;它为全局变量提供了一个本地化的值。它不是您需要经常使用的东西。
  • 避免为子例程提供与系统函数相同的名称(例如print)。这令人困惑。
  • 不建议&在调用子程序之前使用(在您的情况下,由于与系统函数名称冲突,这是必要的,但正如我所说,应该避免)。