Does PHP have threading?

前端 未结 13 1772
孤城傲影
孤城傲影 2020-11-22 09:39

I found this PECL package called threads, but there is not a release yet. And nothing is coming up on the PHP website.

13条回答
  •  故里飘歌
    2020-11-22 10:24

    pcntl_fork() is what you are searching for, but its process forking not threading. so you will have the problem of data exchange. to solve them you can use phps semaphore functions ( http://www.php.net/manual/de/ref.sem.php ) message queues may be a bit easier for the beginning than shared memory segments.

    Anyways, a strategy i am using in a web framework that i am developing which loads resource intensive blocks of a web page (probably with external requests) parallel: i am doing a job queue to know what data i am waiting for and then i fork off the jobs for every process. once done they store their data in the apc cache under a unique key the parent process can access. once every data is there it continues. i am using simple usleep() to wait because inter process communication is not possible in apache (children will loose the connection to their parents and become zombies...). so this brings me to the last thing: its important to self kill every child! there are as well classes that fork processes but keep data, i didn't examine them but zend framework has one, and they usually do slow but reliably code. you can find it here: http://zendframework.com/manual/1.9/en/zendx.console.process.unix.overview.html i think they use shm segments! well last but not least there is an error on this zend website, minor mistake in the example.

    while ($process1->isRunning() && $process2->isRunning()) {
        sleep(1);
    }
    should of course be:
    while ($process1->isRunning() || $process2->isRunning()) {
        sleep(1);
    }
    

提交回复
热议问题