简体   繁体   中英

PHP pthreads is not working from Command Line Interface

I have successfully installed pthreads extension in WAMP.

I received output when I run the Notifications.php from browser.

But, when I run the same code from command line it showing me an error.

I had copied the pthreadVC2.dll in below folders.

D:\wamp\bin\php\php5.5.12
D:\wamp\bin\apache\apache2.4.9\bin

从浏览器 从命令行

Notifications.php

<?php

/*
 * In this example, you will see how to make a process and thread synchronize with each other
 */

/* this is just so reading the logic makes sense */

function do_some_work($m) {
    usleep($m);
}

class ExampleThread extends Thread {
    /*
     * always set defaults for threaded objects in constructors
     * note: using entry level defaults (at the class declaration)
     *   does not work as expected in pthreads, this is because
     *   handlers are not invoked to set defaults at the class level
     */

    public function __construct() {

    }

    public function run() {
        while (!$this->isWaiting()) {
            /* the process is not yet waiting */
            /* in the real world, this would indicate
              that the process is still working */
            /* in the real world, you might have work to do here */
            echo ".";
        }
        echo "\n";

        /* always synchronize before calling notify/wait */
        $this->synchronized(function($me) {
            /* there's no harm in notifying when no one is waiting */
            /* better that you notify no one than deadlock in any case */
            $me->notify();
        }, $this);
    }

}

/* construct the new thread */
$t = new ExampleThread();

/* start the new thread */
if ($t->start()) {
    printf("\nProcess Working ...\n");
    do_some_work(1000);

    /* synchronize in order to call wait */
    $t->synchronized(function($me) {
        /*
         * note: do not stay synchronized for longer than you must
         *   this is to reduce contention for the lock 
         *   associated with the threads internal state
         */
        printf("\nProcess Waiting ...\n");
        $me->wait();
        printf("Process Done ...\n");
    }, $t);
}
?>

I got a solution for this question.

use the below command to find out which configuration file (php.ini) is loading in command line.

> php -i

In my case the file path is:

D:\wamp\bin\php\php5.5.12\php.ini

So, I enabled the php_pthreads.dll extension using below line.

extension = php_pthreads.dll

It might help you.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM