Php multithread

5 Cevap php

Php, gerçekten Multithread için yapılmış değil ama php konuları ile başa çıkmak için herhangi Workarround var.

5 Cevap

"Gözlerini Poke" yoluyla "hemen hemen Tamam Hmmm," değişen birkaç çözüm vardır.

  1. Bir PHP extension olarak okuyuculu kod yazın. Muhtemelen en çok desteklenen, ancak C (veya uzantıları için desteklenen başka bir dilde) yazmak gerekir.
  2. Spawn çocuk temel OS üzerine işler ve standart dosya kolları ile giriş / çıkış okuma / yazma. popen bölgesindeki bir yoldan, bakınız, ya da PCNTRL. Makul ve PHP'nin komut satırından kullanabilirsiniz.
  3. Böylece web sunucuları çoklu-işlem kapasitesi piggybackingle, CURL veya benzeri üzerinden kendinize başka HTTP isteklerini yapmak. Tüm kodunuzu "gibi web" tutar, ama aşağı izleme ve başparmak kıran kızgın destek geliştiriciler riskini taşır.

Özellikle bu için http://www.php.net/manual/en/book.pthreads.php: Bu google en iyi sonuç olduğu için, php, yeni bir uzantısı, pthreads vardır.

PCNTL library edin. Bu, bazı iplik davranışını taklit etmek için yardımcı olabilir.

Ayrıca orada this class:

"This class can emulate the execution of program threads using separate HTTP requests to the same script.

It establishes an HTTP connection to the same Web server to execute the same PHP script. It sends a request passing the name a function to execute and an argument to be passed to that function.

The requested script executes some code that detects the thread execution request and calls the specified function.

When the thread request script ends, the return values of the called function is returned as a serialized string.

The calling script can execute other tasks while the thread script runs. The results may be collected later when the thread script ends."

Bunları da iletişim kurmak için boru veya yuvalarını kullanarak işlemleri yerine konuları kullanabilirsiniz.

Çok iş parçacığı aynı anda birden fazla görevleri veya işlemleri yapmak demektir, php çoklu ulaşmak için doğrudan bir yol olmasına rağmen biz, aşağıdaki kodu kullanarak php bu elde edebilirsiniz ama biz bir şekilde takip ederek, hemen hemen aynı sonuçları elde edebilirsiniz.

chdir(dirname(__FILE__));  //if you want to run this file as cron job
 for ($i = 0; $i < 2; $i += 1){
 exec("php test_1.php $i > test.txt &");
 //this will execute test_1.php and will leave this process executing in the background and will go         

 //to next iteration of the loop immediately without waiting the completion of the script in the   

 //test_1.php , $i  is passed as argument .

}

Test_1.php

$conn=mysql_connect($host,$user,$pass);
$db=mysql_select_db($db);
$i = $argv[1];  //this is the argument passed from index.php file
for($j = 0;$j<5000; $j ++)
{
mysql_query("insert  into  test   set

                id='$i',

                comment='test',

                datetime=NOW() ");

}

Bu şekilde size php çoklu elde edebilirsiniz böylece, bu aynı anda iki kez test_1.php yürüteceğini ve her iki işlem aynı anda arka planda çalışacaktır.

Bu adam gerçekten iyi bir iş yapmış Multithreading in php