forked from krakjoe/pthreads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.php
58 lines (49 loc) · 1.49 KB
/
Singleton.php
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
<?php
/*
* Often an application will have singleton-like objects, such as PDO connections or such.
*
* You cannot pass such objects into a Thread, because they do not gracefully or properly serialize
* themselves and are not Threaded objects.
*
* Even if you could pass the object to a Thread, it would not be safe; They were never intended to be used that
* way and don't have the machinery that Threaded objects have that make them safe to share.
*
* This may seem awkward, but the solution can be quite elegant, and is usually easy to achieve.
*
* This example shows the execution of some Collectables (the anon class) that require a connection to PDO.
*/
class PDOWorker extends Worker {
/*
* Note that we only pass in the configuration to create the connection
*/
public function __construct(array $config) {
$this->config = $config;
}
public function run() {
self::$connection =
new PDO(...$this->config);
}
public function getConnection() { return self::$connection; }
private $config;
/*
* static variables are treated as thread-local by pthreads
*/
private static $connection;
}
/*
* When the Pool starts new Worker threads, they will construct the
* PDO object before any Collectable objects are executed.
*/
$pool = new Pool(4, PDOWorker::class, [["sqlite:example.db"]]);
/*
* Now we can submit work to the Pool
*/
while (@$i++<10) {
$pool->submit(new class extends Threaded {
public function run() {
var_dump($this->worker->getConnection());
}
});
}
$pool->shutdown();
?>