Symfony component process php process

The Process Component

Warning: You are browsing the documentation for Symfony 2.2, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

The Process Component

The Process component executes commands in sub-processes.

Installation

You can install the component in 2 different ways:

Usage

The Process class allows you to execute a command in a sub-process:

use Symfony\Component\Process\Process; $process = new Process('ls -lsa'); $process->run(); // executes after the command finishes if (!$process->isSuccessful()) < throw new \RuntimeException($process->getErrorOutput()); > print $process->getOutput();

The component takes care of the subtle differences between the different platforms when executing the command.

The getIncrementalOutput() and getIncrementalErrorOutput() methods were added in Symfony 2.2.

The getOutput() method always return the whole content of the standard output of the command and getErrorOutput() the content of the error output. Alternatively, the getIncrementalOutput() and getIncrementalErrorOutput() methods returns the new outputs since the last call.

Getting real-time Process Output

When executing a long running command (like rsync-ing files to a remote server), you can give feedback to the end user in real-time by passing an anonymous function to the run() method:

use Symfony\Component\Process\Process; $process = new Process('ls -lsa'); $process->run(function ($type, $buffer) < if (Process::ERR === $type) < echo 'ERR > '.$buffer; > else < echo 'OUT > '.$buffer; > >);

The non-blocking feature was added in 2.1.

Running Processes Asynchronously

You can also start the subprocess and then let it run asynchronously, retrieving output and the status in your main process whenever you need it. Use the start() method to start an asynchronous process, the isRunning() method to check if the process is done and the getOutput() method to get the output:

$process = new Process('ls -lsa'); $process->start(); while ($process->isRunning()) < // waiting for process to finish > echo $process->getOutput();

You can also wait for a process to end if you started it asynchronously and are done doing other stuff:

1 2 3 4 5 6 7 8 9 10 11 12
$process = new Process('ls -lsa'); $process->start(); // . do other things $process->wait(function ($type, $buffer) < if (Process::ERR === $type) < echo 'ERR > '.$buffer; > else < echo 'OUT > '.$buffer; > >);

Stopping a Process

Any asynchronous process can be stopped at any time with the stop() method. This method takes a timeout as its argument. Once the timeout is reached, the process is terminated.

$process = new Process(‘ls -lsa’); $process->start();

// . do other things

$process->stop(3);

Executing PHP Code in Isolation

If you want to execute some PHP code in isolation, use the PhpProcess instead:

use Symfony\Component\Process\PhpProcess; $process = new PhpProcess( EOF ); $process->run();

The ProcessBuilder class was added in Symfony 2.1.

To make your code work better on all platforms, you might want to use the ProcessBuilder class instead:

use Symfony\Component\Process\ProcessBuilder; $builder = new ProcessBuilder(array('ls', '-lsa')); $builder->getProcess()->run();

Process Timeout

You can limit the amount of time a process takes to complete by setting a timeout (in seconds):

use Symfony\Component\Process\Process; $process = new Process('ls -lsa'); $process->setTimeout(3600); $process->run();

If the timeout is reached, a RuntimeException is thrown.

For long running commands, it is your responsibility to perform the timeout check regularly:

$process->setTimeout(3600); $process->start(); while ($condition) < // . // check if the timeout is reached $process->checkTimeout(); usleep(200000); >

Источник

Process Component

Help Symfony by sponsoring the development of this package.
In exchange, we’ll display the logo and description of your company in this section.

Installation

Use Composer to install this component in your PHP project:

Main OSS Projects Using Symfony Process

Drupal

Joomla!

Sulu

Grav

Ibexa DXP

Bolt

PHP Insights

Behat

phpspec

Codeception

PHP Censor

Magento

PrestaShop

Sylius

EC-CUBE

Thelia

Spryker

OroCommerce

Laravel

Symfony

Yii

Infection

Shopware

OroCRM

Akeneo PIM

Pimcore

CiviCRM

PHP Coding Standards Fixer

Google Cloud Platform SDK

Assetic

BotMan

bowerphp

Carew

Composer

RoboTask

Zippy

Rocketeer

Automate

Wikimedia

Deployer

Doctrine

Drupal Console

ownCloud

Crunz

Silex

Lumen

OroPlatform

phpBB

phpDocumentor

Sculpin

Bref

Sami

Sismo

Contao

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Thanks Dmitry Derepko for being a Symfony contributor

2 commits • 104 lines changed

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Symfony™ is a trademark of Symfony SAS. All rights reserved.

What is Symfony?

Learn Symfony

Screencasts

Community

Blog

Services

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Executes commands in sub-processes

License

symfony/process

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

* 6.2: [PhpUnitBridge] Kill the last concurrent process when it stales for more than 60s [Intl] fix test [Intl] Use VarExporter::export() in PhpBundleWriter Use triggering class to generate baseline for deprecation messages from DebugClassLoader [Security] Fix false-string handling in RememberMeAuthenticator [CssSelector] Tests on Xpath translator will always pass [Serializer] Fix Normalizer not utilizing converted name for index variadic param [DepdencyInjection] Fix costly logic when checking errored definitions Fix merge fix children cond [DoctrineBridge] Load refreshed user proxy [DependencyInjection] Don't ignore attributes on the actual decorator [FrameworkBundle] Prevent `cache:clear` to lose files on subsequent runs

Git stats

Files

Failed to load latest commit information.

README.md

The Process component executes commands in sub-processes.

About

Executes commands in sub-processes

Источник

Читайте также:  Узнать имя компьютера python
Оцените статью