-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Refactor parallel && for Windows compatibility
- Loading branch information
Showing
21 changed files
with
588 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © 2024 cclilshy | ||
* Email: [email protected] | ||
* | ||
* This software is licensed under the MIT License. | ||
* For full license details, please visit: https://opensource.org/licenses/MIT | ||
* | ||
* By using this software, you agree to the terms of the license. | ||
* Contributions, suggestions, and feedback are always welcome! | ||
*/ | ||
|
||
use Ripple\Parallel\Parallel; | ||
|
||
use function Co\wait; | ||
|
||
include 'vendor/autoload.php'; | ||
|
||
$parallel = Parallel::getInstance(); | ||
$function = function ($input) { | ||
\sleep(1); | ||
return $input; | ||
}; | ||
|
||
$futures = []; | ||
for ($i = 0; $i < 100; $i++) { | ||
$futures[] = $future = $parallel->run($function, ['name']); | ||
} | ||
|
||
foreach ($futures as $future) { | ||
echo \microtime(true) , ':', $future->value() , \PHP_EOL; | ||
} | ||
|
||
wait(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,148 @@ | ||
<?php | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © 2024 cclilshy | ||
* Email: [email protected] | ||
* | ||
* This software is licensed under the MIT License. | ||
* For full license details, please visit: https://opensource.org/licenses/MIT | ||
* | ||
* By using this software, you agree to the terms of the license. | ||
* Contributions, suggestions, and feedback are always welcome! | ||
*/ | ||
|
||
namespace Ripple\Parallel; | ||
|
||
class Future { | ||
use parallel\Runtime; | ||
use Ripple\WaitGroup; | ||
use Throwable; | ||
|
||
use function extension_loaded; | ||
|
||
if (!extension_loaded('parallel')) { | ||
return; | ||
} | ||
|
||
class Future | ||
{ | ||
public const STATUS_PENDING = 0; | ||
public const STATUS_FULFILLED = 1; | ||
public const STATUS_REJECTED = 2; | ||
|
||
/*** @var \Ripple\Coroutine\WaitGroup */ | ||
private WaitGroup $waitGroup; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private int $status = Future::STATUS_PENDING; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private mixed $result; | ||
|
||
/** | ||
* @param \parallel\Future $parallelFuture | ||
* @param \parallel\Runtime $runtime | ||
*/ | ||
public function __construct(private readonly \parallel\Future $parallelFuture, private readonly Runtime $runtime) | ||
{ | ||
$this->waitGroup = new WaitGroup(1); | ||
} | ||
|
||
/** | ||
* @param mixed $result | ||
* | ||
* @return void | ||
*/ | ||
public function resolve(mixed $result): void | ||
{ | ||
$this->status = Future::STATUS_FULFILLED; | ||
$this->result = $result; | ||
$this->waitGroup->done(); | ||
} | ||
|
||
/** | ||
* @param Throwable $exception | ||
* | ||
* @return void | ||
*/ | ||
public function reject(Throwable $exception): void | ||
{ | ||
$this->status = Future::STATUS_REJECTED; | ||
$this->result = $exception; | ||
$this->waitGroup->done(); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getStatus(): int | ||
{ | ||
return $this->status; | ||
} | ||
|
||
/** | ||
* @return \parallel\Future | ||
*/ | ||
public function getParallelFuture(): \parallel\Future | ||
{ | ||
return $this->parallelFuture; | ||
} | ||
|
||
/** | ||
* @return \parallel\Runtime | ||
*/ | ||
public function getRuntime(): Runtime | ||
{ | ||
return $this->runtime; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function done(): bool | ||
{ | ||
$this->waitGroup->wait(); | ||
return $this->status === Future::STATUS_FULFILLED; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @throws Throwable | ||
*/ | ||
public function value(): mixed | ||
{ | ||
if (!$this->done()) { | ||
throw $this->result; | ||
} | ||
return $this->result; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function cancel(): bool | ||
{ | ||
$bool = $this->getParallelFuture()->cancel(); | ||
Parallel::getInstance()->poll(); | ||
return $bool; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function canceled(): bool | ||
{ | ||
return $this->getParallelFuture()->cancelled(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function kill(): void | ||
{ | ||
$this->getRuntime()->kill(); | ||
Parallel::getInstance()->poll(); | ||
} | ||
} |
Oops, something went wrong.