PHP+fork(): How to run a fork in a PHP code

前端 未结 3 1305
無奈伤痛
無奈伤痛 2021-02-04 19:41

I am running my code on CodeIgniter - Ubuntu Server.

I have been researching for async ways to run functions.

This is my function:



        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-04 19:53

    As far as I am aware of, you can achieve this in two ways...

    Use:

  • Pthreads
  • Amphp
  • Pthreads is a parallel processing library, while amp is a pure asynchronous framework...

    So, the way for using pthreads would be to first download/enable the pthreads extension and add extension=/path/to/pthread.so in the php.ini file...

    And then create a class, which extends the Thread Class and override the method run, and put everything inside it, which you want to do parallely.

    So for your specific purpose the the class could be something like this:

    db_con = $db_connection;
            $this->data = data;
        }
    
        private function run() {
            // use your logic to insert the data...
        }
    }
    

    To use that, just instantiate the class, and put the the DB connection variable, and the data to be processed in the ctor. And call the start method of the object. Like:

    $inserter = new Inserter($dbConn, $data);
    $inserter->start();
    

    Where $dbConn stores the DB connection and $data stores the necessary data.

    And that's it...

提交回复
热议问题