-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Process] Allow running multiple commands at once #43162
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
Comments
In non-windows environments, we're currently replacing the current shell (that PHP starts automatically) with the command to run by prepending it with For chained commands this cannot work because there is no shell to start the subsequent commands ( Ideas and insights are welcome. |
Sounds like a good idea to me. |
I'm not sure I would recommend adding |
You mean a builder that creates shell system specific shell commands that are then treated as "raw input" like with We could also introduce a new $process = new Process([
'foo', Operator::chain(), 'bar', Operator::and(), 'baz'
]); So the array of commands would be |
Why would we loose anything? In the end, the only way to achieve what you describe is to build a shell command line, there is no other way. So yes, I'm thinking about a builder that would generate a string that is then provided to |
Another idea: since PHP 7.4, |
I get your point. My thinking was: We're only adding
👍 I'll have a look. |
We could achieve that by wrapping the call to
BTW, I'm sure we can cleanup the code on branch 6.0 by leveraging this! PR welcome :) |
Thank you for this suggestion. |
Could I get a reply or should I close this? |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
I just tested this: test code used: $start = microtime(true);
for ($i = 0; $i < 1000; ++$i) {
proc_close(proc_open(['/usr/bin/true'], [], $pipes));
}
var_dump(microtime(true) - $start); // 1.34 ms
$start = microtime(true);
for ($i = 0; $i < 1000; ++$i) {
proc_close(proc_open('/usr/bin/true', [], $pipes));
}
var_dump(microtime(true) - $start); // 3.03 ms
$start = microtime(true);
proc_close(proc_open('/usr/bin/true'.str_repeat(' && /usr/bin/true', 999), [], $pipes));
var_dump(microtime(true) - $start); // 0.96 ms As the performance gain of using @nicolas-grekas 2023-11-02 UPDATE: PHP 8.3 brings another 2x speedup to the array version, down to 0.7ms on my system. |
This PR was submitted for the 6.3 branch but it was merged into the 6.4 branch instead. Discussion ---------- Remove unused code from Process | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT While trying to work on #43162 (comment) I noticed that the private member `useFileHandles` is not used for anything anymore. It is always set in the constructor to `'\\' === \DIRECTORY_SEPARATOR` so it basically has a constant value and its only usage is in an `elseif` after a check to the very same constant value: `if ('\\' === \DIRECTORY_SEPARATOR)`. Therefore this code does nothing and can be removed. Commits ------- 7a5eba2 [Process] Remove dead code from Process
… (ausi) This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Process] Pass the commandline as array to `proc_open()` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #43162 (comment) | License | MIT Since PHP 7.4, `proc_open()` accepts an array which makes it twice as fast (4x as fast with PHP 8.3), see #43162 (comment) This pull request enables calling `proc_open()` with an array if: 1. `$this->commandline` is an array 2. and we are not on Windows 3. and PHP was compiled without `--enable-sigchild` Commits ------- a300de8 [Process] Pass the commandline as array to `proc_open()`
… (ausi) This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Process] Pass the commandline as array to `proc_open()` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix symfony/symfony#43162 (comment) | License | MIT Since PHP 7.4, `proc_open()` accepts an array which makes it twice as fast (4x as fast with PHP 8.3), see symfony/symfony#43162 (comment) This pull request enables calling `proc_open()` with an array if: 1. `$this->commandline` is an array 2. and we are not on Windows 3. and PHP was compiled without `--enable-sigchild` Commits ------- a300de8615e [Process] Pass the commandline as array to `proc_open()`
Description
When running a lot of small shell commands after each other, the overhead of creating the underlying processes becomes noticeable. It would be handy if the process component would support chaining commands.
Working directly on a Linux/Windows shell you would typically do this:
(Yes there are other chaining operators as well like
||
.)This is currently not possible with the process component.
API Example
I like that
Symfony\Component\Process\Process
became somewhat immutable now (the command line is baked in after the constructor ran), so the API could maybe look like this:What do you think? Is this a worthwhile addition?
The text was updated successfully, but these errors were encountered: