-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spawning PHP sub-processes in Web Workers (#1069)
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 `spawnHandler` in `worker-thread.ts` that 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.](#1027) This code snippet illustrates the idea – note the actual implementation is more nuanced: ```ts php.setSpawnHandler( createSpawnHandler(async function (args, processApi) { const childPHP = new WebPHP(); const { exitCode, stdout, stderr } = await childPHP.run({ scriptPath: args[1] }); processApi.stdout(stdout); processApi.stderr(stderr); processApi.exit(exitCode); }) ); ``` ## Future work * Stream `stdout` and `stderr` from `childPHP` to `processApi` instead of buffering the output and passing everything at once ## Example of how it works <img width="500" src="https://github.com/WordPress/wordpress-playground/assets/205419/470d79b2-2f10-4f1a-806c-5f26463766da" /> #### /wordpress/spawn.php ```php <?php echo "<plaintext>"; echo "Spawning /wordpress/child.php\n"; $handle = proc_open('php /wordpress/child.php', [ 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'], ], $pipes); echo "stdout: " . stream_get_contents($pipes[1]) . "\n"; echo "stderr: " . stream_get_contents($pipes[2]) . "\n"; echo "Finished\n"; echo "Contents of the created file: " . file_get_contents("/wordpress/new.txt") . "\n"; ``` #### /wordpress/child.php ```php <?php echo "<plaintext>"; echo "Spawned, running"; error_log("Here's a message logged to stderr! " . rand()); file_put_contents("/wordpress/new.txt", "Hello, world!" . rand() . "\n"); ``` ## Testing instructions 1. Update `worker-thread.ts` to create the two files listed above 2. In Playground, navigate to `/spawn.php` 3. Confirm the output is the same as on the screenshot above
- Loading branch information
Showing
60 changed files
with
2,120 additions
and
94 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
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Oops, something went wrong.