Does PHP have threading?

前端 未结 13 1539
孤城傲影
孤城傲影 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:21

    You can use pcntl_fork() to achieve something similar to threads. Technically it's separate processes, so the communication between the two is not as simple with threads, and I believe it will not work if PHP is called by apache.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-11-22 10:24

    There is the rather obscure, and soon to be deprecated, feature called ticks. The only thing I have ever used it for, is to allow a script to capture SIGKILL (Ctrl+C) and close down gracefully.

    0 讨论(0)
  • 2020-11-22 10:27

    There is a Threading extension being activley developed based on PThreads that looks very promising at https://github.com/krakjoe/pthreads

    0 讨论(0)
  • 2020-11-22 10:28

    In short: yes, there is multithreading in php but you should use multiprocessing instead.

    Backgroud info: threads vs. processes

    There is always a bit confusion about the distinction of threads and processes, so i'll shortly describe both:

    • A thread is a sequence of commands that the CPU will process. The only data it consists of is a program counter. Each CPU core will only process one thread at a time but can switch between the execution of different ones via scheduling.
    • A process is a set of shared resources. That means it consists of a part of memory, variables, object instances, file handles, mutexes, database connections and so on. Each process also contains one or more threads. All threads of the same process share its resources, so you may use a variable in one thread that you created in another. If those threads are parts of two different processes, then they cannot access each others resources directly. In this case you need inter-process communication through e.g. pipes, files, sockets...

    Multiprocessing

    You can achieve parallel computing by creating new processes (that also contain a new thread) with php. If your threads do not need much communication or synchronization, this is your choice, since the processes are isolated and cannot interfere with each other's work. Even if one crashes, that doesn't concern the others. If you do need much communication, you should read on at "multithreading" or - sadly - consider using another programming language, because inter-process communication and synchronization introduces a lot of complexion.

    In php you have two ways to create a new process:

    let the OS do it for you: you can tell your operation system to create a new process and run a new (or the same) php script in it.

    • for linux you can use the following or consider Darryl Hein's answer:

      $cmd = 'nice php script.php 2>&1 & echo $!';
      pclose(popen($cmd, 'r'));
      
    • for windows you may use this:

      $cmd = 'start "processname" /MIN /belownormal cmd /c "script.php 2>&1"';
      pclose(popen($cmd, 'r'));
      

    do it yourself with a fork: php also provides the possibility to use forking through the function pcntl_fork(). A good tutorial on how to do this can be found here but i strongly recommend not to use it, since fork is a crime against humanity and especially against oop.

    Multithreading

    With multithreading all your threads share their resources so you can easily communicate between and synchronize them without a lot of overhead. On the other side you have to know what you are doing, since race conditions and deadlocks are easy to produce but very difficult to debug.

    Standard php does not provide any multithreading but there is an (experimental) extension that actually does - pthreads. Its api documentation even made it into php.net. With it you can do some stuff as you can in real programming languages :-) like this:

    class MyThread extends Thread {
        public function run(){
            //do something time consuming
        }
    }
    
    $t = new MyThread();
    if($t->start()){
        while($t->isRunning()){
            echo ".";
            usleep(100);
        }
        $t->join();
    }
    

    For linux there is an installation guide right here at stackoverflow's.

    For windows there is one now:

    • First you need the thread-safe version of php.
    • You need the pre-compiled versions of both pthreads and its php extension. They can be downloaded here. Make sure that you download the version that is compatible with your php version.
    • Copy php_pthreads.dll (from the zip you just downloaded) into your php extension folder ([phpDirectory]/ext).
    • Copy pthreadVC2.dll into [phpDirectory] (the root folder - not the extension folder).
    • Edit [phpDirectory]/php.ini and insert the following line

      extension=php_pthreads.dll
      
    • Test it with the script above with some sleep or something right there where the comment is.

    And now the big BUT: Although this really works, php wasn't originally made for multithreading. There exists a thread-safe version of php and as of v5.4 it seems to be nearly bug-free but using php in a multi-threaded environment is still discouraged in the php manual (but maybe they just did not update their manual on this, yet). A much bigger problem might be that a lot of common extensions are not thread-safe. So you might get threads with this php extension but the functions you're depending on are still not thread-safe so you will probably encounter race conditions, deadlocks and so on in code you did not write yourself...

    0 讨论(0)
  • 2020-11-22 10:31

    Here is an example of what Wilco suggested:

    $cmd = 'nohup nice -n 10 /usr/bin/php -c /path/to/php.ini -f /path/to/php/file.php action=generate var1_id=23 var2_id=35 gen_id=535 > /path/to/log/file.log & echo $!';
    $pid = shell_exec($cmd);
    

    Basically this executes the PHP script at the command line, but immediately returns the PID and then runs in the background. (The echo $! ensures nothing else is returned other than the PID.) This allows your PHP script to continue or quit if you want. When I have used this, I have redirected the user to another page, where every 5 to 60 seconds an AJAX call is made to check if the report is still running. (I have a table to store the gen_id and the user it's related to.) The check script runs the following:

    exec('ps ' . $pid , $processState);
    if (count($processState) < 2) {
         // less than 2 rows in the ps, therefore report is complete
    }
    

    There is a short post on this technique here: http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

    0 讨论(0)
提交回复
热议问题