Repository navigation
Expand file tree
/
Copy pathFunctions.php
More file actions
83 lines (71 loc) · 1.92 KB
/
Copy pathFunctions.php
File metadata and controls
83 lines (71 loc) · 1.92 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
<?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 Closure;
use Hyperf\Context\ApplicationContext;
use RuntimeException;
use Swoole\Runtime;
/**
* @param callable[] $callables
* @param int $concurrent if $concurrent is equal to 0, that means unlimited
*/
function parallel(array $callables, int $concurrent = 0): array
{
$parallel = new Parallel($concurrent);
foreach ($callables as $key => $callable) {
$parallel->add($callable, $key);
}
return $parallel->wait();
}
/**
* @template TReturn
*
* @param Closure():TReturn $closure
* @return TReturn
*/
function wait(Closure $closure, ?float $timeout = null)
{
if (ApplicationContext::hasContainer()) {
$waiter = ApplicationContext::getContainer()->get(Waiter::class);
return $waiter->wait($closure, $timeout);
}
return (new Waiter())->wait($closure, $timeout);
}
function co(callable $callable): bool|int
{
$id = Coroutine::create($callable);
return $id > 0 ? $id : false;
}
function defer(callable $callable): void
{
Coroutine::defer($callable);
}
function go(callable $callable): bool|int
{
$id = Coroutine::create($callable);
return $id > 0 ? $id : false;
}
/**
* Run callable in non-coroutine environment, all hook functions by Swoole only available in the callable.
*
* @param array|callable $callbacks
*/
function run($callbacks, int $flags = SWOOLE_HOOK_ALL): bool
{
if (Coroutine::inCoroutine()) {
throw new RuntimeException('Function \'run\' only execute in non-coroutine environment.');
}
Runtime::enableCoroutine($flags);
/* @phpstan-ignore-next-line */
$result = \Swoole\Coroutine\run(...(array) $callbacks);
Runtime::enableCoroutine(0);
return $result;
}