수색…


비고

pthreads 사용하면 cli SAPI를 사용할 때만 pthreads 를로드 할 수 있으므로 PHP7 및 Pthreads v3을 사용하는 경우 extension=pthreads.so 지시어를 php-cli.ini 에 유지하는 것이 좋습니다.

Windows 에서 Wamp 를 사용하고 있다면, php.ini 에서 확장 기능을 설정해야합니다 :

php \ php.ini 파일을 열고 다음을 추가하십시오 :

extension=php_pthreads.dll

Linux 사용자의 경우 .dll.so 로 대체해야합니다.

extension=pthreads.so

이 명령을 직접 실행하여 php.ini 에 추가 할 수 있습니다 (사용자 정의 경로로 /etc/php.ini 변경).

echo "extension=pthreads.so" >> /etc/php.ini

시작하기

멀티 스레딩을 시작하려면, php를위한 pthreads-ext 가 필요합니다.

$ pecl install pthreads

항목을 php.ini 추가하십시오.

간단한 예 :

<?php
// NOTE: Code uses PHP7 semantics.
class MyThread extends Thread {
    /**
     * @var string
     * Variable to contain the message to be displayed.
     */
    private $message;
    
    public function __construct(string $message) {
        // Set the message value for this particular instance.
        $this->message = $message;
    }

    // The operations performed in this function is executed in the other thread.
    public function run() {
        echo $this->message;
    }
}

// Instantiate MyThread
$myThread = new MyThread("Hello from an another thread!");
// Start the thread. Also it is always a good practice to join the thread explicitly.
// Thread::start() is used to initiate the thread,
$myThread->start();
// and Thread::join() causes the context to wait for the thread to finish executing
$myThread->join();

풀 및 근로자 사용

풀링은 pthreads가 요구하는 방식으로 참조 관리를 포함하여 Worker 기능의 추상화를 제공합니다. 보낸 사람 : http://php.net/manual/en/class.pool.php

풀 및 작업자는 높은 수준의 제어 및 멀티 스레드 생성 용이성을 제공합니다.

<?php
// This is the *Work* which would be ran by the worker.
// The work which you'd want to do in your worker.
// This class needs to extend the \Threaded or \Collectable or \Thread class.
class AwesomeWork extends Thread {
    private $workName;

    /**
     * @param string $workName
     * The work name wich would be given to every work.
     */
    public function __construct(string $workName) {
        // The block of code in the constructor of your work,
        // would be executed when a work is submitted to your pool.

        $this->workName = $workName;
        printf("A new work was submitted with the name: %s\n", $workName);
    }

    public function run() {
        // This block of code in, the method, run
        // would be called by your worker.
        // All the code in this method will be executed in another thread.
        $workName = $this->workName;
        printf("Work named %s starting...\n", $workName);
        printf("New random number: %d\n", mt_rand());
    }
}

// Create an empty worker for the sake of simplicity.
class AwesomeWorker extends Worker {
    public function run() {
        // You can put some code in here, which would be executed
        // before the Work's are started (the block of code in the `run` method of your Work)
        // by the Worker.
        /* ... */
    }
}

// Create a new Pool Instance.
// The ctor of \Pool accepts two parameters.
// First: The maximum number of workers your pool can create.
// Second: The name of worker class.
$pool = new \Pool(1, \AwesomeWorker::class);

// You need to submit your jobs, rather the instance of
// the objects (works) which extends the \Threaded class.
$pool->submit(new \AwesomeWork("DeadlyWork"));
$pool->submit(new \AwesomeWork("FatalWork"));

// We need to explicitly shutdown the pool, otherwise,
// unexpected things may happen.
// See: http://stackoverflow.com/a/23600861/23602185
$pool->shutdown();


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow