2015-11-19 175 views
0

我必須執行100多個php腳本才能並行執行,每個php腳本都會在數據庫中插入新數據並更新以前的一些記錄。並行執行多個php腳本

例如,每個腳本網址將是這樣的:

example.com/update.php?data-from-project-1.com
example.com/update.php?data-from- project-2.com
example.com/update.php?data-from-project-2.com
等等...

和更新頁面的工作是這樣的:
Update.php

<?php 
//insert some new records from project data 
//update some records 
?> 

我試圖做的事:
cron作業PAGE:

$urls=array("data-from-project-2.com", "data-from-project-2.com",....) 
for ($index = 0; $index < count($urls); $index++) { 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "update.php?$urls[$index]"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_exec($ch); 
curl_close($ch); } 

,但我想這樣做是爲了執行所有的網頁,並且能夠產生超時問題爲好。無論如何,通過cron作業頁面並行執行它們。我不需要任何輸出,他們只需要更新數據庫。任何幫助,將不勝感激。謝謝

回答

0

使用PHP主題。簡單示例:

<?php 

class workerThread extends Thread { 
public function __construct($i){ 
    $this->i=$i; 
} 

public function run(){ 
    while(true){ 
    echo $this->i; 
    sleep(1); 
    } 
} 
} 

for($i=0;$i<50;$i++){ 
$workers[$i]=new workerThread($i); 
$workers[$i]->start(); 
} 

?> 

您可以在每個線程中寫入每個更新。但我提供你爲每個線程創建一些批次。因爲每個線程也用一些時間來啓動。 或者每個線程可能使用全局數組變量「url」的一部分(start_index,end_index)