SORU
16 EYLÜL 2008, Salı


Nasıl bir PHP uygulamalarında çoklu iş parçacığı kullanabilirsiniz

Olsun gerçekten PHP, yoksa sadece onu taklit ederken çok kanallı bir modeli hayata gerçekçi bir yolu yoktur. Bir süre önce işletim sistemi, PHP başka bir örneği yürütülebilir yük ve diğer eş zamanlı işlemler işlemek için güç olabilir önerildi.

Bu sorun PHP kodu PHP örneği yürütme bittiğinde PHP içinden onu öldürmek için bir yol yoktur çünkü bellekte kalır. Eğer öyleyse birçok konuları taklit ederse neler olacağını tahmin edersiniz. Hala Çoklu-işlem bitti mi yoksa PHP içinde etkili gelen taklit etmek için bir yol arıyorum. Herhangi bir fikir?

CEVAP
19 Mart 2013, Salı


Çoklu php mümkündür

Evet pthreads multi-threading in PHP yapabilirsiniz

PHP DOC

pthreads kullanıcı-arazi sağlayan Nesne Odaklı bir API Çoklu-işlem PHP. Çok iş parçacıklı uygulamalar veya Konsol Web hedef oluşturmak için gereken tüm araçları içerir. PHP uygulamaları oluşturmak, yazma, çalıştırma ve iş Parçacığı, İşçi ve Stackables ile senkronize okuyabilirsiniz.

Basit Bir Test

#!/usr/bin/php
<?php
class AsyncOperation extends Thread {

    public function __construct($arg) {
        $this->arg = $arg;
    }

    public function run() {
        if ($this->arg) {
            $sleep = mt_rand(1, 10);
            printf('%s: %s  -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep);
            sleep($sleep);
            printf('%s: %s  -finish' . "\n", date("g:i:sa"), $this->arg);
        }
    }
}

// Create a array
$stack = array();

//Iniciate Miltiple Thread
foreach ( range("A", "D") as $i ) {
    $stack[] = new AsyncOperation($i);
}

// Start The Threads
foreach ( $stack as $t ) {
    $t->start();
}

?>

İlk Çalıştırma

12:00:06pm:     A  -start -sleeps 5
12:00:06pm:     B  -start -sleeps 3
12:00:06pm:     C  -start -sleeps 10
12:00:06pm:     D  -start -sleeps 2
12:00:08pm:     D  -finish
12:00:09pm:     B  -finish
12:00:11pm:     A  -finish
12:00:16pm:     C  -finish

İkinci Çalıştırın

12:01:36pm:     A  -start -sleeps 6
12:01:36pm:     B  -start -sleeps 1
12:01:36pm:     C  -start -sleeps 2
12:01:36pm:     D  -start -sleeps 1
12:01:37pm:     B  -finish
12:01:37pm:     D  -finish
12:01:38pm:     C  -finish
12:01:42pm:     A  -finish

Gerçek Dünya Örnek

error_reporting(E_ALL);
class AsyncWebRequest extends Thread {
    public $url;
    public $data;

    public function __construct($url) {
        $this->url = $url;
    }

    public function run() {
        if (($url = $this->url)) {
            /*
             * If a large amount of data is being requested, you might want to
             * fsockopen and read using usleep in between reads
             */
            $this->data = file_get_contents($url);
        } else
            printf("Thread #%lu was not provided a URL\n", $this->getThreadId());
    }
}

$t = microtime(true);
$g = new AsyncWebRequest(sprintf("http://www.google.com/?q=%s", rand() * 10));
/* starting synchronized */
if ($g->start()) {
    printf("Request took %f seconds to start ", microtime(true) - $t);
    while ( $g->isRunning() ) {
        echo ".";
        usleep(100);
    }
    if ($g->join()) {
        printf(" and %f seconds to finish receiving %d bytes\n", microtime(true) - $t, strlen($g->data));
    } else
        printf(" and %f seconds to finish, request failed\n", microtime(true) - $t);
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • GFX Tutorials

    GFX Tutorial

    12 AĞUSTOS 2013
  • RaquelGamesBR

    RaquelGamesB

    20 HAZİRAN 2009
  • Tianna Sierra Dance

    Tianna Sierr

    16 EYLÜL 2013