PHP-并行任务运行器 [英] PHP - parallel task runner

查看:94
本文介绍了PHP-并行任务运行器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要PHP中的并行任务运行程序(最有可能在Windows上-IIS7 fastcgi),它具有完整的实现隐藏.它的界面应该是这样的:

I need a parallel task runner in PHP (most likely on windows - IIS7 fastcgi), which has a complete implementation hiding. Its interface should be something like this:

$taskRunner = new ParallelTaskRunner();
$taskRunner->add(function () use ($sharedResource){
    //task 1
});
$taskRunner->add(function () use ($sharedResource){
    //task 2
});
$taskRunner->run(); //runs task 1, task 2 parallel

我已经做过一些研究:我知道可以并行运行PHP代码的技术-pthreads,pcntl,exec,gearman,curl multi等...

I have done some research: I am aware of techniques with which I can run PHP code parallel - pthreads, pcntl, exec, gearman, curl multi, etc...

我需要回答的问题:

  • 是否可以使用任何这些技术隐藏此实现?如何做到这一点(如果实现细节中有一些高级解决方法)?有没有具有此功能的库?
  • 如何提取有关并行任务运行的信息?例如:task1: { a(); b(); }task2: { c(); },我怎么知道函数的调用顺序是a(); b(); c();a(); c(); b();c(); a(); b();?
  • 如果出现问题,如何调试任务?
  • Is it possible to make this implementation hiding with any of these techniques? How is it possible to do that (if there is some advanced workaround in implementation details)? Is there any library which has this feature?
  • How can I extract the information about the running of the parallel tasks? For example: task1: { a(); b(); }, task2: { c(); }, how can I know that the calling order of the functions was a(); b(); c(); or a(); c(); b(); or c(); a(); b();?
  • How can I debug my tasks if something goes wrong?

更新

我检查了许多可能的解决方案,最后得到了pthreads.使用线程比使用进程容易得多.唯一的缺点是它不支持xdebug.

I checked many possible solution, and I ended up with pthreads. It is much easier to use threads than processes... The only drawback that it does not support xdebug.

推荐答案

在PHP中带有pthreads的承诺

我写的东西,但是没有时间谈论...

承诺是美味的,它们使通常很复杂的东西变成非常简单的东西.

Promises are delicious, they make something ordinarily rather complex into something very simple.

当我们谈论Javascript时,每个人都了解堆栈的概念;这就是动画以及我们在网络上所做的许多其他奇特的事情的运行方式.

Everyone understands the concept of a stack when we talk about Javascript; this is how animations, and lots of the other fancy stuff we do on the web, are run.

实际上,我为pthreads编写promise的尝试是试图使线程变得像每个人都理解的Javascript堆栈一样简单.

In effect, my effort to write promises for pthreads is an attempt to make threading as simple as a Javascript stack, which everyone understands.

碰巧的是,这东西几乎可以完全满足您的需求:

It just so happens that this very thing suits your needs almost perfectly:

$manager = new PromiseManager();
$promise = 
    new Promise($manager, new CalculateTheMeaningOfLife());
$promise
    ->then(
        new AddTwo($promise))
    ->then(
        new PrintMeaning($promise));

$manager->shutdown();

您必须使用对象而不是闭包(Zend的限制),但这是promises模式,它是完全异步和托管的.

You do have to use objects and not closures (limitation of Zend), but it is the promises pattern and it is fully async and managed.

这是PHP,因此可以与composer一起安装...如果您喜欢这种东西...

This is PHP, so can be installed with composer ... if you like that sort of thing ...

https://github.com/krakjoe/promises

这篇关于PHP-并行任务运行器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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