generated from kpicaza/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
allow fork server in threads #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Antidot\React; | ||
|
|
||
| use RuntimeException; | ||
| use function pcntl_fork; | ||
| use function pcntl_waitpid; | ||
|
|
||
| class Child | ||
| { | ||
| private const DEFAULT_NUMBER_OF_FORKS = 0; | ||
|
|
||
| public static function fork( | ||
| int $numberOfWorkers, | ||
| callable $asyncServer, | ||
| int $numberOfFork = self::DEFAULT_NUMBER_OF_FORKS | ||
| ): int { | ||
| if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | ||
| throw new RuntimeException('The PHP pcntl extension is not available for Windows systems'); | ||
| } | ||
|
|
||
| $pid = pcntl_fork(); | ||
| if (-1 === $pid) { | ||
| throw new RuntimeException('Fork Failed'); | ||
| } | ||
|
|
||
| if (0 === $pid) { | ||
| $asyncServer(); | ||
| pcntl_waitpid($pid, $status); | ||
| return $pid; | ||
| } | ||
|
|
||
| // @parent | ||
| $numberOfWorkers--; | ||
| ++$numberOfFork; | ||
| if (self::DEFAULT_NUMBER_OF_FORKS < $numberOfWorkers) { | ||
| self::fork($numberOfWorkers, $asyncServer, $numberOfFork); | ||
| } | ||
|
|
||
| pcntl_waitpid($pid, $status); | ||
| return $pid; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace AntidotTest\React; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Process\Process; | ||
|
|
||
| class ChildTest extends TestCase | ||
| { | ||
| public function testItShouldBeConstructedStatically(): void | ||
| { | ||
| $process = new Process([ | ||
| 'php', | ||
| '-r', | ||
| 'include "src/Child.php";\Antidot\React\Child::fork(1, function() { echo "test passed"; }, 0);' | ||
| ]); | ||
| $process->start(); | ||
| $process->wait(); | ||
| $this->assertSame('test passed', $process->getOutput()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just notice that the
pcntl_forkfunction is only available on Unix-based system.The reference is available here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am searching for an alternative for windows without luck, this feature will only be available on nix systems I think. We can protect it from usage in Windows systems;-S
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like this:

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm working o mutation tests now;- D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, do you consider using the
pthread?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peter279k @mmoreram I've tried parallel extension with @xserrat a time ago, and we discard it for complexity issues(we make it run with a docker image created by @WyriHaximus) antidot-framework/antidot-react-psr15#1, both pthreads and parallel requires PHP compiled with ZTS and extra configuration(enable extension).
I propose to maintain this feature without the windows multi-thread support and opening a new issue to research and then add multi-thread support in windows systems.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check this. The
Parallelextension looks good :).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kpicaza In the short term that's the easiest way to make this happen. Really interested to see how well this works with different event loops as this isn't something we've tried in the past.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peter279k Yeah, I've build https://github.com/reactphp-parallel around it for a reason 😉 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @WyriHaximus, @mmoreram, and @peter279k for guidance;- D. I've just opened a new issue #14 to add support and document the usage by parallel ext.