forked from phpDocumentor/Parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
81 lines (67 loc) · 3.04 KB
/
example.php
File metadata and controls
81 lines (67 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* DocBlox
*
* PHP Version 5
*
* @category DocBlox
* @package Parallel
* @author Mike van Riel <mike.vanriel@naenius.com>
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://docblox-project.org
*/
/** Include the manager as we do not autoload */
require_once 'Manager.php';
/** Include the worker as we do not autoload */
require_once 'Worker.php';
/** Include the worker's pipe as we do not autoload */
require_once 'WorkerPipe.php';
// -----------------------------------------------------------------------------
// method 1: using a fluent interface and the addWorker helper.
// -----------------------------------------------------------------------------
$mgr = new DocBlox_Parallel_Manager();
$mgr
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(1); return 'a'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(1); return 'b'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(1); return 'c'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(1); return 'd'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(1); return 'e'; }))
->execute();
/** @var DocBlox_Parallel_Worker $worker */
foreach ($mgr as $worker) {
var_dump($worker->getResult());
}
// -----------------------------------------------------------------------------
// method 2: using the manager as worker array
// -----------------------------------------------------------------------------
$mgr = new DocBlox_Parallel_Manager();
$mgr[] = new DocBlox_Parallel_Worker(function() { sleep(1); return 'f'; });
$mgr[] = new DocBlox_Parallel_Worker(function() { sleep(1); return 'g'; });
$mgr[] = new DocBlox_Parallel_Worker(function() { sleep(1); return 'h'; });
$mgr[] = new DocBlox_Parallel_Worker(function() { sleep(1); return 'i'; });
$mgr[] = new DocBlox_Parallel_Worker(function() { sleep(1); return 'j'; });
$mgr->execute();
/** @var DocBlox_Parallel_Worker $worker */
foreach ($mgr as $worker) {
var_dump($worker->getResult());
}
// -----------------------------------------------------------------------------
// Extra test: demonstrating timing with callbacks
// This should finish in 8 seconds without callback, and 5 seconds with
// (Tested on quad core)
// -----------------------------------------------------------------------------
$time = microtime(true);
$mgr = new DocBlox_Parallel_Manager();
$mgr
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(5); return 'k'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(4); return 'l'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(4); return 'm'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(2); return 'n'; }))
->addWorker(new DocBlox_Parallel_Worker(function() { sleep(3); return 'o'; }))
->execute();
/** @var DocBlox_Parallel_Worker $worker */
foreach ($mgr as $worker) {
var_dump($worker->getResult());
}
echo 'Time: ' . (microtime(true) - $time) . PHP_EOL;