如何在 Perl 中编写并行程序? [英] How to write parallel programs in Perl?

查看:61
本文介绍了如何在 Perl 中编写并行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在一个 Perl 脚本中完成多项任务.最好不要使用 forkthread.

I have to do several tasks in one Perl script. Better not use fork or thread.

  1. 任务 A:每 5 秒向服务器发出一次 HTTP 请求.无穷大,不应被阻止.如果得到:

  1. task A: make HTTP request to a server every 5 seconds. Infinity and should not be blocked. And if gets:

  1. 'TASK',任务A会调用一个耗时的子进程,那么如何避免赋值执行阻塞任务A的循环?
  2. 'PAUSE',暂停请求
  3. 'UPDATE', 告诉任务 B 做某事
  4. 从任务 B 或任务 C 接收数据时向服务器发出 POST 请求

  • 任务 B:每 1 分钟发出一次 mysql 请求,根据结果,将 告诉任务 A 做 POST 请求

  • task B: make mysql request every 1 minute, and depend on results, will tell task A to do POST request

    任务 C:接受套接字连接并告诉任务 A 或 B 做某事.

    task C: accept socket connection and tell task A or B to do something.

    三个并行的不定式循环进程,并且会相互通信.我怎样才能做到这一点?

    Three parallel infinitive loop processes, and will communication with each other. How can I do that?

    推荐答案

    这种设计毫无意义,而且声称最好不要使用线程或子进程的说法更没有意义.

    That design makes no sense whatsoever, and the claim that it's better not to use threads or child processes makes even less sense.

    您有三个请求来源:

    • 请求源 A:每 5 秒向服务器发出一次 Web 请求.
    • 请求源 B:每 60 秒向服务器发出一次数据库请求.
    • 请求源 C:接受来自套接字的请求.

    为每个请求源创建一个线程.他们的工作仅仅是监控每个请求源,以确保在应该检查源时检查源.因此,这些线程都不应该做任何实际工作.如果必须执行任务,它们会将工作委托给工作线程.他们不发布任何内容.它们不写入数据库.

    Create a thread for each Request Source. Their job is solely to monitor each Request Source in order to ensure that the Sources are checked when they should be checked. As such, none of these threads should do any real work. If a task must be performed, they delegate the work to a worker thread. They don't POST anything. They don't write to the database.

    实际任务(包括发送 POST 和写入数据库)由一个或多个工作线程(您的选择)执行.工作线程从由三个请求源填充的单个 Thread::Queue 队列接收请求.

    The actual tasks (including sending POSTs and writing to the database) are performed by one or more worker threads (your choice). The worker threads receive requests from a singular Thread::Queue queue populated by the three Request Sources.

    所以代码看起来像:

    use threads;
    use Thread::Queue qw( );
    
    use constant NUM_WORKERS => 5;  # Tweak this. Can be as low as 1.
    
    sub poll_web {
       my ($request_q) = @_;
       ... init ...
       while (1) {
          ...
          $request_q->enqueue([post => ...]);
          ...
       }
    }
    
    sub poll_db { ... }              # Just like poll_web
    
    sub accept_connections { ... }   # Just like poll_web
    
    sub post_handler { ... }         # Receives args passed to enqueue
    

    {
       my $request_q = Thread::Queue->new();
    
       my %job_handlers = (
          post => \&post_handler,
          ...
       );
    
       for (1..NUM_WORKERS) {
          async {
             while (1) {
                my $job = $request_q->dequeue();
                my ($job_type, @args) = @$job;
                my $handler = $job_handlers{$job_type};
                   or do { warn("Unknown job type $job_type"); next };
                $handler->(@args);
             }
          };
       }
    
       async { poll_web($request_q); };
       async { poll_db($request_q); };
       accept_connections($request_q);
    }
    

    <小时>

    如果要使用进程而不是线程,请更改


    If you want to use processes instead of threads, change

    use threads;
    

    use forks;
    

    但继续使用线程::队列.

    but keep using Thread::Queue.

    这篇关于如何在 Perl 中编写并行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆