Skip to content

Commit 463b226

Browse files
committed
add async helper function
1 parent bdf06c7 commit 463b226

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"skrtdev\\async\\": "src/"
1818
},
1919
"files":[
20-
"src/range.php"
20+
"src/range.php",
21+
"src/helpers.php"
2122
]
2223
}
2324
}

examples/simple.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
require '../vendor/autoload.php';
3+
4+
async(function(){
5+
sleep(3);
6+
echo 'Wow, this seems to be async', PHP_EOL;
7+
});
8+
9+
echo 'Hello world', PHP_EOL;

src/Pool.php

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Pool{
1616
private bool $is_resolving_queue = false;
1717
private bool $need_tick = true;
1818

19+
protected static self $default_pool;
20+
1921
public function __construct(?int $max_childs = null, bool $kill_childs = true)
2022
{
2123
if(!extension_loaded('pcntl')){
@@ -254,6 +256,11 @@ public static function isProcessRunning(int $pid): bool
254256
return posix_getpgid($pid) !== false;
255257
}
256258

259+
public static function getDefaultPool(): self
260+
{
261+
return static::$default_pool ??= new static();
262+
}
263+
257264
}
258265

259266

src/helpers.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
use skrtdev\async\Pool;
4+
5+
if(!function_exists('async')){
6+
function async(callable $callable, ...$args){
7+
Pool::getDefaultPool()->parallel($callable, ...$args);
8+
}
9+
}

0 commit comments

Comments
 (0)