Repository navigation
Expand file tree
/
Copy pathCoroutine.php
More file actions
145 lines (128 loc) · 3.89 KB
/
Copy pathCoroutine.php
File metadata and controls
145 lines (128 loc) · 3.89 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Coroutine;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Engine\Coroutine as Co;
use Hyperf\Engine\Exception\CoroutineDestroyedException;
use Hyperf\Engine\Exception\RunningInNonCoroutineException;
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Throwable;
class Coroutine
{
/**
* Returns the current coroutine ID.
* Returns -1 when running in non-coroutine context.
*/
public static function id(): int
{
return Co::id();
}
public static function defer(callable $callable): void
{
Co::defer(static function () use ($callable) {
try {
$callable();
} catch (Throwable $throwable) {
static::printLog($throwable);
}
});
}
public static function sleep(float $seconds): void
{
usleep(intval($seconds * 1000 * 1000));
}
/**
* Returns the parent coroutine ID.
* Returns 0 when running in the top level coroutine.
* @throws RunningInNonCoroutineException when running in non-coroutine context
* @throws CoroutineDestroyedException when the coroutine has been destroyed
*/
public static function parentId(?int $coroutineId = null): int
{
return Co::pid($coroutineId);
}
/**
* The alias of Coroutine::parentId().
* @throws CoroutineDestroyedException when running in non-coroutine context
* @throws RunningInNonCoroutineException when the coroutine has been destroyed
*/
public static function pid(?int $coroutineId = null): int
{
return Co::pid($coroutineId);
}
/**
* @return int Returns the coroutine ID of the coroutine just created.
* Returns -1 when coroutine create failed.
*/
public static function create(callable $callable): int
{
$coroutine = Co::create(static function () use ($callable) {
try {
$callable();
} catch (Throwable $throwable) {
static::printLog($throwable);
}
});
try {
return $coroutine->getId();
} catch (Throwable) {
return -1;
}
}
/**
* Create a coroutine with a copy of the parent coroutine context.
*/
public static function fork(callable $callable, array $keys = []): int
{
$cid = static::id();
$callable = static function () use ($callable, $cid, $keys) {
Context::copy($cid, $keys);
$callable();
};
return static::create($callable);
}
public static function inCoroutine(): bool
{
return Co::id() > 0;
}
public static function stats(): array
{
return Co::stats();
}
public static function exists(int $id): bool
{
return Co::exists($id);
}
/**
* @return iterable<int>
*/
public static function list(): iterable
{
return Co::list();
}
private static function printLog(Throwable $throwable): void
{
if (ApplicationContext::hasContainer()) {
$container = ApplicationContext::getContainer();
if ($container->has(StdoutLoggerInterface::class)) {
$logger = $container->get(StdoutLoggerInterface::class);
if ($container->has(FormatterInterface::class)) {
$formatter = $container->get(FormatterInterface::class);
$logger->warning($formatter->format($throwable));
} else {
$logger->warning((string) $throwable);
}
}
}
}
}