Skip to content


PHP ก็สามารถทำ Multithreading/Parrallel-programing ได้

เมื่อวานได้อ่านเรื่องการใช้ทำ multithreading ใน php เลยคิดว่าน่าสนใจ (http://codestips.com/php-multithreading-using-curl/) เพราะโดยปกติแล้วตัว php เองไม่สามารถทำการฟอร์ค thread ขึ้นมาได้เอง จึงทำการให้การทำงานในบางประเภททำได้ยากหรือไม่มีประสิทธิภาพ ซึ่งเมื่อได้อ่านแล้วก็รู้สึกว่ามีประโยชน์เป็นอย่างมาก ซึ่งอาจจะเขียน php_proxy ใน Napzer library ให้เป็นอย่างนี้ก็คงดี อย่างไรก็ตามผมยังห่วงเรื่องประสิทธิภาพในการทำงานว่ามันจะมีประสิทธิภาพมากแค่ไหน ซึ่งผมคิดว่ามันขึ้นอยู่กับการออกแบบ ซึ่งคงต้องลองทำการทดสอบกันดูในระดับหนึ่ง

จาก WikiBall

การเขียนแอพลิเคชั่นแบบ Parallel programming ใน PHP นั้นไม่สามารถทำได้โดยตรง เพราะ PHP ไม่มีความยุ่งยากในการจัดการเกี่ยวกับเรื่องของ threading จึงทำให้ง่ายต่อการเขียน แต่สำหรับงานบางอย่างแล้วการใช้การคำนวณแบบคูขนานก็มีความจำเป็นในการใช้งาน ไม่ว่าจะเป็นการทำงานอยู่บนเครื่องเดียวกัน หรือเครื่องอื่นก็ตาม เราจึงต้องหาวิธีการอื่นมาช่วย ซึ่งในที่นี้เราจะพูดถึงการใช้ cURL library มาช่วยในการจัดการ

เนื้อหา



ข้อดีของการทำ parrallel programming

การเขียนโปรแกรมแบบ parrallel นั้นเหมาะสมกับการทำงานบางอย่าง ซึ่งในคอมพิวเตอร์ของเราในปัจจุบันก็ใช้การทำงานลักษณะดังกล่าวในโปรแกรมหลายๆ ชนิด เช่น โปรแกรมทางด้านกราฟิก วิธีการนี้จะช่วยลดเวลาในการทำงานของกระบวนการหลักไปได้เป็นอย่างมาก เช่น เราแบ่งส่วนต่างๆ ของหน้าเวปของเราเป็น widget หรือ module ย่อยๆ แล้วในเครื่องแต่ละเครื่องทำงานในการสร้างโมดูลของตัวเองโดยเฉพาะ ซึ่งจะดีแค่ไหนที่เราสามารถเรียกใช้งาน module ต่างๆ นั้นได้พร้อมๆ กัน โดยไม่ต้องรอโหลดเรียงกันทีละ module โดยการเขียนโปรแกรมอย่างนี้จะทำให้ช่วยประหยัดเวลาสร้างข้อมูลที่จะต้องส่งให้ client ได้มาก และยังสามารถใช้ทรัพยากรที่มีอยู่ได้อย่างมีประสิทธิภาพสูงสุดอีกด้วย


uCURL กับการทำ multithreading

uCURL นั้นโดยปกติแล้วไม่ถูกติดตั้งเข้ามาใน PHP มาก่อนแแต่จะมาเป็น extension เสริมสำหรับ PHP (โดยส่วนมากผู้ให้บริการ Host ทั่วไปจะทำการติดตั้งมาให้อยู่แล้ว) หนึ่งในความสามารถของ uCURL ที่น่าสนใจอย่างมากคือการทำ multi handle ซึ่งยอมให้สามารถทำ multiple cURL processing ในแบบ parallel ซึ่งฟังก์ชั่นสำคัญที่ต้องใข้ได้แก่

  • curl_multi_init – initialize a new cURL multi handle. It will return the cURL handle on success and FALSE on error.
  • curl_multi_add_handle — Add a cURL handle to a cURL multi handle.
  • curl_multi_exec — Runs all the curl handle in the cURL multi handle in parallel.
  • curl_multi_remove_handle — Removes a cURL handle from a cURL multi handle.
  • curl_multi_close — close the cURL multi handle.


ตัวอย่างการใช้ cURL

สร้างอเรย์สำหรับบ่งบอกว่า process ไหนจะทำที่ url ใด

$process[1] = "http://www.example1.com";
$process[2] = "ftp://example3.com";
$process[3] = "http://www.example2.com";

สร้าง cURL และทำการกำหนด handles

$curlHandle = curl_multi_init();
for ($i = 1;$i <= 3; $i++)
 $curl[$i] = addHandle($curlHandle,$process[$i]);

รัน process

ExecHandle($curlHandle);

รับ content จาก handle ต่างๆ

for ($i = 1;$i <= 3; $i++)
{
 $content[$i] =  curl_multi_getcontent ($curl[$i]);
 echo $content[$i];
}

ปิด handles และ url

for ($i = 1;$i <= 3; $i++)//remove the sub - handles
  curl_multi_remove_handle($curlHandle,$curl[$i]);
curl_multi_close($curlHandle);


ตัวอย่าง

<?
//add a url to the handler
function addHandle(&$curlHandle,$url)
{
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($curlHandle,$cURL);
return $cURL;
}
//execute the handle until the flag passed
// to function is greater then 0
function ExecHandle(&$curlHandle)
{
$flag=null;
do {
//fetch pages in parallel
    curl_multi_exec($curlHandle,$flag);
} while ($flag > 0);
}

$list[1] = "http://www.example1.com";
$list[2] = "ftp://example.com";
$list[3] = "http://www.example2.com";
$curlHandle = curl_multi_init();
for ($i = 1;$i <= 3; $i++)
 $curl[$i] = addHandle($curlHandle,$list[$i]);
ExecHandle($curlHandle);
for ($i = 1;$i <= 3; $i++)
{
 $text[$i] =  curl_multi_getcontent ($curl[$i]);
 echo $text[$i];
}
for ($i = 1;$i <= 3; $i++)//remove the handles
  curl_multi_remove_handle($curlHandle,$curl[$i]);
curl_multi_close($curlHandle);
?>

จาก http://codestips.com/php-multithreading-using-curl/

Complete source (Mirror)
ทำตัวหนา

รับข้อมูลจาก (Reference) :

จาก WikiBall : http://wiki.memoball.info/index.php/PHP_multithreading_and_Parallel_programming_using_cURL

Posted in PHP, Programming, Web Application. Tagged with .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.