Describes how to use PHP swoole process, swooleprocess
Background: If we have 10000 orders generated every day, we need to synchronize them to the warehouse system. In the past, we started a crontab to run these tasks, but we found that the synchronization efficiency is always low, the interval is minute-level.
Solution test: we add a hash as the key in the task table of the Synchronization order as the distribution condition, because if the select statement in mysql does not use the index for mod function, therefore, we do random hash on our own, but we do not need to have a large scope to avoid insufficient server resources. The method is to place the hashkey in different processes for synchronization. The test code is as follows:
<? Php/*** Created by PhpStorm. * User: xujun * Date: * Time: * // assume that the data to be processed is as follows class Process {public $ mpid = 0; public $ max_precess = 5; // Replace the public $ task = [['uid' => 1, 'uname' => 'bot ', 'hash' => 1, 'handle' => 'test'], ['uid' => 2, 'uname' => 'bot1', 'hash' => 2, 'handle' => 'test'], ['uid' => 3, 'uname' => 'bot2', 'hash' => 3, 'handle' => 'test'], ['uid' => 4, 'uname' => 'bot3 ', 'hash' => 4, 'handle' => 'test'], ['uid' => 2, 'uname' => 'bot4 ', 'hash' => 2, 'handle' => 'test'], ['uid' => 3, 'uname' => 'bot5 ', 'hash' => 3, 'handle' => 'test'], ['uid' => 4, 'uname' => 'bot6 ', 'hash' => 1, 'handle' => 'test'],]; public $ works = []; public $ swoole_table = NULL; // public $ new_index = 0; function test ($ index, $ task) {print_r ("[". date ('Y-m-d H: I: s '). "]". 'Work-index :'. $ index. 'process '. $ task ['uname']. 'complete '. PHP_EOL);} public function _ construct () {try {$ this-> Swoole_table = new swoole_table (1024); $ this-> swoole_table-> column ('index', swoole_table: TYPE_INT ); // used for data exchange between parent and child processes $ this-> swoole_table-> create (); swoole_set_process_name (sprintf ('php-ps: % s', 'master ')); $ this-> mpid = posix_getpid (); $ this-> run (); $ this-> processWait ();} catch (\ Exception $ e) {die ('all ERROR :'. $ e-> getMessage () ;}} public function run () {for ($ I = 0; $ I <$ this-> max_precess; $ I ++ ){ $ This-> CreateProcess () ;}} private function getTask ($ index) {$ _ return = []; foreach ($ this-> task as $ v) {if ($ v ['hash'] ==$ index) {$ _ return [] = $ v ;}} return $ _ return ;} public function CreateProcess ($ index = null) {if (is_null ($ index) {// if no index is specified, the newly created sub-process, enable count $ index = $ this-> swoole_table-> get ('index'); if ($ index = false) {$ index = 0 ;} else {$ index = $ index ['index'] + 1;} print_r ($ index);} $ this-> sw Oole_table-> set ('index', array ('index' => $ index); $ process = new swoole_process (function (swoole_process $ worker) use ($ index) {swoole_set_process_name (sprintf ('php-ps: % s', $ index); $ task = $ this-> getTask ($ index); foreach ($ task as $ v) {call_user_func_array (array ($ this, $ v ['handle']), array ($ index, $ v) ;} sleep (20) ;}, false, false ); $ pid = $ process-> start (); $ this-> works [$ index] = $ pid; return $ pid;} publ Ic function rebootProcess ($ ret) {$ pid = $ ret ['pid ']; $ index = array_search ($ pid, $ this-> works); if ($ index! = False) {$ index = intval ($ index); $ new_pid = $ this-> CreateProcess ($ index); echo "rebootProcess: {$ index }={ $ new_pid} Done \ n "; return;} throw new \ Exception ('rebootprocess Error: no pid ');} public function processWait () {while (1) {if (count ($ this-> works) {$ ret = swoole_process: wait (); if ($ ret) {$ this-> rebootProcess ($ ret) ;}} else {break ;}}}$ process = new Process ();
In this Code, swoole_table is used as the memory shared between processes, in order to allocate an index. And when the process exits, the parent process re-pulls the process task through wait.
Test
Process ps
The result is automatically pulled up after 20 s of sleep.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.