Commit 24f2c61
authored
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 above1 parent 0eba044 commit 24f2c61
File tree
60 files changed
+2120
-94
lines changed- packages
- php-wasm
- compile/php
- node/public
- 7_0_33
- 7_1_30
- 7_2_34
- 7_3_33
- 7_4_33
- 8_0_30
- 8_1_23
- 8_2_10
- 8_3_0
- universal/src/lib
- util/src/lib
- web/public
- kitchen-sink
- 7_0_33
- 7_1_30
- 7_2_34
- 7_3_33
- 7_4_33
- 8_0_30
- 8_1_23
- 8_2_10
- 8_3_0
- light
- 7_0_33
- 7_1_30
- 7_2_34
- 7_3_33
- 7_4_33
- 8_0_30
- 8_1_23
- 8_2_10
- 8_3_0
- playground/remote/src/lib
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
60 files changed
+2120
-94
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
812 | 812 | | |
813 | 813 | | |
814 | 814 | | |
| 815 | + | |
815 | 816 | | |
816 | 817 | | |
817 | 818 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
954 | 954 | | |
955 | 955 | | |
956 | 956 | | |
957 | | - | |
| 957 | + | |
958 | 958 | | |
959 | 959 | | |
960 | 960 | | |
| |||
1175 | 1175 | | |
1176 | 1176 | | |
1177 | 1177 | | |
1178 | | - | |
1179 | | - | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
1180 | 1185 | | |
1181 | 1186 | | |
1182 | 1187 | | |
| |||
1434 | 1439 | | |
1435 | 1440 | | |
1436 | 1441 | | |
1437 | | - | |
| 1442 | + | |
1438 | 1443 | | |
1439 | 1444 | | |
1440 | 1445 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
19 | 24 | | |
20 | 25 | | |
21 | 26 | | |
| |||
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.
0 commit comments