Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Php_swoole 's basic operation on the process

Shulou Source: shulou.com Published: 2022-06-02 04:54:34 10月19日 Update

This article focuses on "the basic operation of php_swoole on the process". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the basic operation of php_swoole on the process".

In previous development projects, the pcntl extension that comes with PHP was used to manipulate the process. But pcntl has many shortcomings:

Pcntl does not provide the function of interprocess communication

Pcntl does not support redirecting standard input and output

Pcntl only provides a primitive interface such as fork, so it is easy to make mistakes.

Here we use the swoole process management module to replace the pcntl of PHP

Scenario: in daily tasks, you sometimes need to perform some log analysis, queue processing and other tasks through php scripts. When the amount of data is relatively large, you can use multiple processes to process.

Ready: install PHP_swoole extension

Write ($data)

Write data to the pipe. The maximum length of $data is not more than 8K under Linux system and not more than 2K under FreeBSD.

Start ():

Execute the fork call to start the child process.

/ * * create a child process, write the data to the pipeline, and then start the promoter process * / $process = new\ swoole_process ([$this, 'sonProcessWork']); $process- > write (json_encode ($data_pop)); $pid = $process- > start ()

Swoole_event_add (): add a socket to the underlying reactor event listener; handle time-consuming calculations or operations in child processes

/ * * obtain the business data of the pipeline and perform related operations * / public function sonProcessWork (\ swoole_process $worker) {$GLOBALS ['worker'] = $worker Swoole_event_add ($worker- > pipe, function ($pipe) {/ * start* your business logic * /. / * end* your business logic * / $worker- > exit (0);})}

Wait (false)

Recycling ends the running child process

/ * * according to the signal processing Forbidden City * / private function sig_handler ($signo) {Process::signal (SIGCHLD, function ($sig) {/ / must be false, non-blocking mode while ($ret = Process::wait (false)) {echo "PID= {$ret ['pid']}\ n";}}) } at this point, I believe you have a deeper understanding of "php_swoole 's basic operation of the process". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Process data processing basic operation business pipeline maximum task content logic learning original practical heavy deeper event signal processing interest function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL Shulou Technology Shulou Tech Info vpn