SwGo.php
1.2 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
<?php
namespace Lib;
/**
* 这个是协程封装
* @author:dc
* @time 2025/3/17 11:22
* Class SwGo
* @package Lib
*/
class SwGo {
/**
* 运行状态
* @var array
*/
public static $runStatus = [];
/**
* 已经运行的go数量
* @var int
*/
public static $runNumber = 0;
/**
*
* @param \Closure $run
* @param \Closure $end
* @author:dc
* @time 2025/3/17 11:35
*/
public static function start(\Closure $run,...$param): bool
{
go(function (\Closure $run,...$param){
self::$runNumber++;
$end = null;
if(empty($param[0]) && $param[0] instanceof \Closure){
$end = $param[0];
unset($param[0]);
$param = array_values($param);
}
$run(...$param);
if($end) $end(...$param);
// 写入日志
\Lib\Log::getInstance()->write();
// 释放 mysql
db()->close();
// 释放redis
redis()->close();
self::$runNumber--;
},$run,...$param);
return true;
}
}