Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Php_swoole 's basic operation on the process

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report