Spawning PHP sub-processes in Web Workers - #1069
Merged
Merged
Conversation
adamziel
added a commit
that referenced
this pull request
Feb 28, 2024
## What is this PR doing? Supersedes #1051 Adds a PHP Blueprints demo page where the use of [blueprints.phar](WordPress/blueprints#28) PHP library in Playground may be further explored. The showcase is intentionally not added to http://localhost:5400/website-server/demos/index.html as PHP Blueprints may become a part of Playground core soon enough. For more context see: * #1025 * https://github.com/WordPress/blueprints ## How does it work? * The built Blueprints library is included with this PR via the `blueprints.phar` file * A number of PHP.wasm improvements have been merged to support it: * #1064 * #1065 * #1069 * This PR ships a `fetch` subprocess handler to enable streaming network data in the Blueprints library – it uses [a special network transport called `fetch`](https://github.com/WordPress/blueprints/blob/efa8deef56095bd5bcb94868787e29f7b54350f3/src/WordPress/DataSource/PlaygroundFetchSource.php) that requests network data via `proc_open()` when running in Playground. Why subprocesses? They provide everything a custom network handler needs: pipes, asynchronous execution, exit codes, internal PHP plumbing. ## Follow-up work * Support a real-time progress bar ## Testing instructions Go to http://localhost:5400/website-server/demos/php-blueprints.html and confirm it looks like on the screenshot below: 
bgrgicak
reviewed
Feb 29, 2024
Comment on lines
+339
to
+350
| if (!childPHP.fileExists(path)) { | ||
| childPHP.mkdir(path); | ||
| } | ||
| childPHP[__private__dont__use].FS.mount( | ||
| childPHP[__private__dont__use].PROXYFS, | ||
| { | ||
| root: path, | ||
| fs: php[__private__dont__use].FS, | ||
| }, | ||
| path | ||
| ); | ||
| } |
Collaborator
There was a problem hiding this comment.
Nice! Glad you found a way without copying the files.
| define('STDIN', fopen('php://stdin', 'rb')); | ||
| define('STDOUT', fopen('php://stdout', 'wb')); | ||
| define('STDERR', fopen('/tmp/stderr', 'wb')); | ||
|
|
Collaborator
There was a problem hiding this comment.
Should this be /internal/stderr?
Collaborator
Author
There was a problem hiding this comment.
Oh! Yeah if that remains a file path then yes. I wonder why this isn't php://stderr actually, I think I copied it over from #161 and, at that time, there was a bug preventing the use of php://stderr.
Collaborator
Author
There was a problem hiding this comment.
I created an issue to track it here: #1071
This was referenced Feb 29, 2024
adamziel
added a commit
to WordPress/playground-tools
that referenced
this pull request
Mar 6, 2024
…ead of /tmp/stderr (#173) Adapts the block to the changes implemented in WordPress/wordpress-playground#1069
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Related to #1026, #1027, and #1051. Supersedes #1031
Adds support for spawning PHP subprocesses via
<?php proc_open(['php', 'activate_theme.php']);. The spawned subprocess affects the filesystem used by the parent process.Implementation details
This PR updates the default
spawnHandlerinworker-thread.tsthat creates another WebPHP instance and mounts the parent filesystem using Emscripten's PROXYFS.A shared filesystem didn't pan out. Synchronizing is the second best option.
This code snippet illustrates the idea – note the actual implementation is more nuanced:
Future work
stdoutandstderrfromchildPHPtoprocessApiinstead of buffering the output and passing everything at onceExample of how it works
/wordpress/spawn.php
/wordpress/child.php
Testing instructions
worker-thread.tsto create the two files listed above/spawn.phpcc @dmsnell @bgrgicak