简体   繁体   English

如何使用Perl同时处理部分任务顺序?

[英]How do I process a partial order of tasks concurrently using Perl?

I have a partially ordered set of tasks, where for each task all of the tasks that are strictly before it in the partial order must be executed before it can be executed. 我有一组部分有序的任务,对于每个任务,在执行之前必须执行在部分顺序之前严格执行的所有任务。 I want to execute tasks which are not related (either before or after one other) concurrently to try to minimise the total execution time - but without starting a task before its dependencies are completed. 我想同时执行不相关的任务(在一个之前或之后)以尝试最小化总执行时间 - 但是在完成依赖之前不启动任务。

The tasks will run as (non-perl) child processes. 任务将作为(非perl)子进程运行。

How should I approach solving a problem like this using Perl? 我应该如何使用Perl解决这样的问题? What concurrency control facilities and data structures are available? 什么是并发控制工具和数据结构?

I would use a hash of arrays. 我会使用数组的哈希。 For each task, all its prerequisities will be mentioned in the corresponding array: 对于每个任务,将在相应的数组中提及其所有先决条件:

$prereq{task1} = [qw/task2 task3 task4/];

I would keep completed tasks in a different hash, and then just 我会将完成的任务保存在不同的哈希中,然后只是

my @prereq = @{ $prereq{$task} };
if (@prereq == grep exists $completed{$_}, @prereq) {
    run($task);
}

Looks like a full solution is NP-complete . 看起来完整的解决方案是NP完全的

As for a partial solution, I would use some form of reference counting to determine which jobs are ready to run, Forks::Super::Job to run the background jobs and check their statuses and POSIX::pause to sleep when maximum number of jobs is spawned. 至于部分解决方案,我会使用某种形式的引用计数来确定哪些作业可以运行, Forks :: Super :: Job运行后台作业并检查其状态, POSIX :: pause睡眠时最大数量为产生了就业机会。

No threads are involved since you're already dealing with separate processes. 由于您已经在处理单独的进程,因此不涉及任何线程。

Read the first link for possible algorithms/heuristics to determine runnable jobs' priorities. 阅读第一个链接,了解可能的算法/启发式算法,以确定可运行作业的优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM