<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MEMOBALL.info &#187; PHP</title>
	<atom:link href="http://www.memoball.info/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.memoball.info</link>
	<description>All of my Memories</description>
	<lastBuildDate>Thu, 22 Jul 2010 06:17:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP ก็สามารถทำ Multithreading/Parrallel-programing ได้</title>
		<link>http://www.memoball.info/05/php-%e0%b8%81%e0%b9%87%e0%b8%aa%e0%b8%b2%e0%b8%a1%e0%b8%b2%e0%b8%a3%e0%b8%96%e0%b8%97%e0%b8%b3-multithreadingparrallel-programing-%e0%b9%84%e0%b8%94%e0%b9%89/</link>
		<comments>http://www.memoball.info/05/php-%e0%b8%81%e0%b9%87%e0%b8%aa%e0%b8%b2%e0%b8%a1%e0%b8%b2%e0%b8%a3%e0%b8%96%e0%b8%97%e0%b8%b3-multithreadingparrallel-programing-%e0%b9%84%e0%b8%94%e0%b9%89/#comments</comments>
		<pubDate>Tue, 26 May 2009 04:21:42 +0000</pubDate>
		<dc:creator>spnball</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Application]]></category>

		<guid isPermaLink="false">http://memoball.nabzer.com/?p=289</guid>
		<description><![CDATA[เมื่อวานได้อ่านเรื่องการใช้ทำ multithreading ใน php เลยคิดว่าน่าสนใจ (http://codestips.com/php-multithreading-using-curl/) เพราะโดยปกติแล้วตัว php เองไม่สามารถทำการฟอร์ค thread ขึ้นมาได้เอง จึงทำการให้การทำงานในบางประเภททำได้ยากหรือไม่มีประสิทธิภาพ ซึ่งเมื่อได้อ่านแล้วก็รู้สึกว่ามีประโยชน์เป็นอย่างมาก ซึ่งอาจจะเขียน php_proxy ใน Napzer library ให้เป็นอย่างนี้ก็คงดี อย่างไรก็ตามผมยังห่วงเรื่องประสิทธิภาพในการทำงานว่ามันจะมีประสิทธิภาพมากแค่ไหน ซึ่งผมคิดว่ามันขึ้นอยู่กับการออกแบบ ซึ่งคงต้องลองทำการทดสอบกันดูในระดับหนึ่ง


จาก WikiBall


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





เนื้อหา


1 ข้อดีของการทำ parrallel programming
2 uCURL กับการทำ multithreading
3 ตัวอย่างการใช้ cURL
4 ตัวอย่าง




 if (window.showTocToggle) { var tocShowText [...]]]></description>
			<content:encoded><![CDATA[<p>เมื่อวานได้อ่านเรื่องการใช้ทำ multithreading ใน php เลยคิดว่าน่าสนใจ (<a href="http://codestips.com/php-multithreading-using-curl/">http://codestips.com/php-multithreading-using-curl/</a>) เพราะโดยปกติแล้วตัว php เองไม่สามารถทำการฟอร์ค thread ขึ้นมาได้เอง จึงทำการให้การทำงานในบางประเภททำได้ยากหรือไม่มีประสิทธิภาพ ซึ่งเมื่อได้อ่านแล้วก็รู้สึกว่ามีประโยชน์เป็นอย่างมาก ซึ่งอาจจะเขียน php_proxy ใน Napzer library ให้เป็นอย่างนี้ก็คงดี อย่างไรก็ตามผมยังห่วงเรื่องประสิทธิภาพในการทำงานว่ามันจะมีประสิทธิภาพมากแค่ไหน ซึ่งผมคิดว่ามันขึ้นอยู่กับการออกแบบ ซึ่งคงต้องลองทำการทดสอบกันดูในระดับหนึ่ง<br />
<span id="more-289"></span></p>
<div id="bodyContent">
<h3 id="siteSub"><a href="http://wiki.memoball.info/index.php/PHP_multithreading_and_Parallel_programming_using_cURL">จาก WikiBall</a></h3>
<div id="contentSub"></div>
<div id="jump-to-nav"></div>
<p>			<!-- start content --></p>
<p>การเขียนแอพลิเคชั่นแบบ Parallel programming ใน PHP นั้นไม่สามารถทำได้โดยตรง เพราะ PHP ไม่มีความยุ่งยากในการจัดการเกี่ยวกับเรื่องของ threading จึงทำให้ง่ายต่อการเขียน แต่สำหรับงานบางอย่างแล้วการใช้การคำนวณแบบคูขนานก็มีความจำเป็นในการใช้งาน ไม่ว่าจะเป็นการทำงานอยู่บนเครื่องเดียวกัน หรือเครื่องอื่นก็ตาม เราจึงต้องหาวิธีการอื่นมาช่วย ซึ่งในที่นี้เราจะพูดถึงการใช้ cURL library มาช่วยในการจัดการ
</p>
<table id="toc" class="toc" summary="เนื้อหา">
<tr>
<td>
<div id="toctitle">
<h2>เนื้อหา</h2>
</div>
<ul>
<li class="toclevel-1"><a href="./#.E0.B8.82.E0.B9.89.E0.B8.AD.E0.B8.94.E0.B8.B5.E0.B8.82.E0.B8.AD.E0.B8.87.E0.B8.81.E0.B8.B2.E0.B8.A3.E0.B8.97.E0.B8.B3_parrallel_programming" ref="toc"><span class="tocnumber">1</span> <span class="toctext">ข้อดีของการทำ parrallel programming</span></a></li>
<li class="toclevel-1"><a href="./#uCURL_.E0.B8.81.E0.B8.B1.E0.B8.9A.E0.B8.81.E0.B8.B2.E0.B8.A3.E0.B8.97.E0.B8.B3_multithreading" ref="toc"><span class="tocnumber">2</span> <span class="toctext">uCURL กับการทำ multithreading</span></a></li>
<li class="toclevel-1"><a href="./#.E0.B8.95.E0.B8.B1.E0.B8.A7.E0.B8.AD.E0.B8.A2.E0.B9.88.E0.B8.B2.E0.B8.87.E0.B8.81.E0.B8.B2.E0.B8.A3.E0.B9.83.E0.B8.8A.E0.B9.89_cURL" ref="toc"><span class="tocnumber">3</span> <span class="toctext">ตัวอย่างการใช้ cURL</span></a></li>
<li class="toclevel-1"><a href="./#.E0.B8.95.E0.B8.B1.E0.B8.A7.E0.B8.AD.E0.B8.A2.E0.B9.88.E0.B8.B2.E0.B8.87" ref="toc"><span class="tocnumber">4</span> <span class="toctext">ตัวอย่าง</span></a></li>
</ul>
</td>
</tr>
</table>
<p><script type="text/javascript"> if (window.showTocToggle) { var tocShowText = "แสดง"; var tocHideText = "ซ่อน"; showTocToggle(); } </script><br />
<a name=".E0.B8.82.E0.B9.89.E0.B8.AD.E0.B8.94.E0.B8.B5.E0.B8.82.E0.B8.AD.E0.B8.87.E0.B8.81.E0.B8.B2.E0.B8.A3.E0.B8.97.E0.B8.B3_parrallel_programming" href="http://wiki.memoball.info/"></a><br />
<h2> <span class="mw-headline"> ข้อดีของการทำ parrallel programming </span></h2>
<p>การเขียนโปรแกรมแบบ parrallel นั้นเหมาะสมกับการทำงานบางอย่าง ซึ่งในคอมพิวเตอร์ของเราในปัจจุบันก็ใช้การทำงานลักษณะดังกล่าวในโปรแกรมหลายๆ ชนิด เช่น โปรแกรมทางด้านกราฟิก วิธีการนี้จะช่วยลดเวลาในการทำงานของกระบวนการหลักไปได้เป็นอย่างมาก เช่น เราแบ่งส่วนต่างๆ ของหน้าเวปของเราเป็น widget หรือ module ย่อยๆ แล้วในเครื่องแต่ละเครื่องทำงานในการสร้างโมดูลของตัวเองโดยเฉพาะ ซึ่งจะดีแค่ไหนที่เราสามารถเรียกใช้งาน module ต่างๆ นั้นได้พร้อมๆ กัน โดยไม่ต้องรอโหลดเรียงกันทีละ module โดยการเขียนโปรแกรมอย่างนี้จะทำให้ช่วยประหยัดเวลาสร้างข้อมูลที่จะต้องส่งให้ client ได้มาก และยังสามารถใช้ทรัพยากรที่มีอยู่ได้อย่างมีประสิทธิภาพสูงสุดอีกด้วย
</p>
<p><a name="uCURL_.E0.B8.81.E0.B8.B1.E0.B8.9A.E0.B8.81.E0.B8.B2.E0.B8.A3.E0.B8.97.E0.B8.B3_multithreading" href="http://wiki.memoball.info/"></a><br />
<h2> <span class="mw-headline"> uCURL กับการทำ multithreading </span></h2>
<p>uCURL นั้นโดยปกติแล้วไม่ถูกติดตั้งเข้ามาใน PHP มาก่อนแแต่จะมาเป็น extension เสริมสำหรับ PHP (โดยส่วนมากผู้ให้บริการ Host ทั่วไปจะทำการติดตั้งมาให้อยู่แล้ว) หนึ่งในความสามารถของ uCURL ที่น่าสนใจอย่างมากคือการทำ multi handle ซึ่งยอมให้สามารถทำ multiple cURL processing ในแบบ parallel ซึ่งฟังก์ชั่นสำคัญที่ต้องใข้ได้แก่
</p>
<ul>
<li> <b>curl_multi_init</b> &#8211; initialize a new cURL multi handle. It will return the cURL handle on success and FALSE on error.
</li>
<li> <b>curl_multi_add_handle</b> — Add a cURL handle to a cURL multi handle.
</li>
<li> <b>curl_multi_exec</b> — Runs all the curl handle in the cURL multi handle in parallel.
</li>
<li> <b>curl_multi_remove_handle</b> — Removes a cURL handle from a cURL multi handle.
</li>
<li> <b>curl_multi_close</b> — close the cURL multi handle.
</li>
</ul>
<p><a name=".E0.B8.95.E0.B8.B1.E0.B8.A7.E0.B8.AD.E0.B8.A2.E0.B9.88.E0.B8.B2.E0.B8.87.E0.B8.81.E0.B8.B2.E0.B8.A3.E0.B9.83.E0.B8.8A.E0.B9.89_cURL" href="http://wiki.memoball.info/"></a><br />
<h2> <span class="mw-headline"> ตัวอย่างการใช้ cURL </span></h2>
<p>สร้างอเรย์สำหรับบ่งบอกว่า process ไหนจะทำที่ url ใด
</p>
<pre name="code" class="php">
$process[1] = &quot;http://www.example1.com&quot;;
$process[2] = &quot;ftp://example3.com&quot;;
$process[3] = &quot;http://www.example2.com&quot;;
</pre>
<p>สร้าง cURL และทำการกำหนด handles
</p>
<pre name="code" class="php">
$curlHandle = curl_multi_init();
for ($i = 1;$i &lt;= 3; $i++)
 $curl[$i] = addHandle($curlHandle,$process[$i]);
</pre>
<p>รัน process
</p>
<pre name="code" class="php">
ExecHandle($curlHandle);
</pre>
<p>รับ content จาก handle ต่างๆ
</p>
<pre name="code" class="php">
for ($i = 1;$i &lt;= 3; $i++)
{
 $content[$i] =  curl_multi_getcontent ($curl[$i]);
 echo $content[$i];
}
</pre>
<p>ปิด handles และ url
</p>
<pre name="code" class="php">
for ($i = 1;$i &lt;= 3; $i++)//remove the sub - handles
  curl_multi_remove_handle($curlHandle,$curl[$i]);
curl_multi_close($curlHandle);
</pre>
<p><a name=".E0.B8.95.E0.B8.B1.E0.B8.A7.E0.B8.AD.E0.B8.A2.E0.B9.88.E0.B8.B2.E0.B8.87" href="http://wiki.memoball.info/"></a><br />
<h2> <span class="mw-headline"> ตัวอย่าง </span></h2>
<pre name="code" class="php">
&lt;?
//add a url to the handler
function addHandle(&amp;$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(&amp;$curlHandle)
{
$flag=null;
do {
//fetch pages in parallel
    curl_multi_exec($curlHandle,$flag);
} while ($flag &gt; 0);
}

$list[1] = &quot;http://www.example1.com&quot;;
$list[2] = &quot;ftp://example.com&quot;;
$list[3] = &quot;http://www.example2.com&quot;;
$curlHandle = curl_multi_init();
for ($i = 1;$i &lt;= 3; $i++)
 $curl[$i] = addHandle($curlHandle,$list[$i]);
ExecHandle($curlHandle);
for ($i = 1;$i &lt;= 3; $i++)
{
 $text[$i] =  curl_multi_getcontent ($curl[$i]);
 echo $text[$i];
}
for ($i = 1;$i &lt;= 3; $i++)//remove the handles
  curl_multi_remove_handle($curlHandle,$curl[$i]);
curl_multi_close($curlHandle);
?&gt;
</pre>
<p>จาก <a href="http://codestips.com/php-multithreading-using-curl/" class="external free" title="http://codestips.com/php-multithreading-using-curl/" rel="nofollow">http://codestips.com/php-multithreading-using-curl/</a>
</p>
<p><a href="http://codestips.com/scripts/multithread.zip" class="external text" title="http://codestips.com/scripts/multithread.zip" rel="nofollow">Complete source</a>  <a href="http://www.memoball.info/wp-content/uploads/2009/05/multithread.zip" class="external text" title="http://www.memoball.info/wp-content/uploads/2009/05/multithread.zip" rel="nofollow">(Mirror)</a><br />
<b>ทำตัวหนา</b>
</p>
<p><!--<br />
NewPP limit report<br />
Preprocessor node count: 23/1000000<br />
Post-expand include size: 0/2097152 bytes<br />
Template argument size: 0/2097152 bytes<br />
Expensive parser function count: 0/100<br />
--></p>
<p><!-- Saved in parser cache with key memoball_wiki:pcache:idhash:55-0!1!0!!th!2!edit=0 and timestamp 20090825091057 --></p>
<div class="printfooter">
<h5>รับข้อมูลจาก (Reference) : </h5>
<p>จาก WikiBall : <a href="http://wiki.memoball.info/index.php/PHP_multithreading_and_Parallel_programming_using_cURL">http://wiki.memoball.info/index.php/PHP_multithreading_and_Parallel_programming_using_cURL</a></div>
<div id='catlinks' class='catlinks'>
<div id="mw-normal-catlinks"><a href="http://wiki.memoball.info//index.php/%E0%B8%9E%E0%B8%B4%E0%B9%80%E0%B8%A8%E0%B8%A9:Categories" title="พิเศษ:Categories">หมวดหมู่</a>:&#32;<span dir='ltr'><a href="http://wiki.memoball.info//index.php?title=%E0%B8%AB%E0%B8%A1%E0%B8%A7%E0%B8%94%E0%B8%AB%E0%B8%A1%E0%B8%B9%E0%B9%88:PHP&amp;action=edit&amp;redlink=1" class="new" title="หมวดหมู่:PHP (ยังไม่ได้สร้าง)">PHP</a></span></div>
</div>
<p>			<!-- end content --></p>
<div class="visualClear"></div>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.memoball.info/05/php-%e0%b8%81%e0%b9%87%e0%b8%aa%e0%b8%b2%e0%b8%a1%e0%b8%b2%e0%b8%a3%e0%b8%96%e0%b8%97%e0%b8%b3-multithreadingparrallel-programing-%e0%b9%84%e0%b8%94%e0%b9%89/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การสร้าง PHP Error Log</title>
		<link>http://www.memoball.info/03/187/</link>
		<comments>http://www.memoball.info/03/187/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 03:20:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://memoball.nabzer.com/?p=187</guid>
		<description><![CDATA[วันนี้ได้ทำการจัดการเรื่อง error ของ php ไม่ให้แสดงบนหน้าเวป แต่ไปเก็บไว้ที่ file แทน ทำให้เพิ่มความปลอดภัยของเวปไซต์เราและยังสามารถทำการ debug โปรแกรมได้ง่ายด้วย 

จาก WikiBall


			
			
 ปิดไม่ให้แสดง error

# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off


 แสดง error ใน logs file

# enable PHP error logging
php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/PHP_errors.log


 จัดการ permission ของ logs

# prevent access to PHP error log
&#60;Files PHP_errors.log&#62;
 Order allow,deny
 Deny from all
 [...]]]></description>
			<content:encoded><![CDATA[<p>วันนี้ได้ทำการจัดการเรื่อง error ของ php ไม่ให้แสดงบนหน้าเวป แต่ไปเก็บไว้ที่ file แทน ทำให้เพิ่มความปลอดภัยของเวปไซต์เราและยังสามารถทำการ debug โปรแกรมได้ง่ายด้วย <span id="more-187"></span></p>
<div id="bodyContent">
<h3 id="siteSub"><a href="http://wiki.memoball.info/index.php/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87_PHP_Error_logs_%E0%B9%83%E0%B8%99_.htaccess">จาก WikiBall</a></h3>
<div id="contentSub"></div>
<div id="jump-to-nav"></div>
<p>			<!-- start content --><br />
			<a name=".E0.B8.9B.E0.B8.B4.E0.B8.94.E0.B9.84.E0.B8.A1.E0.B9.88.E0.B9.83.E0.B8.AB.E0.B9.89.E0.B9.81.E0.B8.AA.E0.B8.94.E0.B8.87_error" href="http://wiki.memoball.info/"></a><br />
<h3> <span class="mw-headline">ปิดไม่ให้แสดง error</span></h3>
<pre>
# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
</pre>
<p><a name=".E0.B9.81.E0.B8.AA.E0.B8.94.E0.B8.87_error_.E0.B9.83.E0.B8.99_logs_file" href="http://wiki.memoball.info/"></a><br />
<h3> <span class="mw-headline">แสดง error ใน logs file</span></h3>
<pre>
# enable PHP error logging
php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/PHP_errors.log
</pre>
<p><a name=".E0.B8.88.E0.B8.B1.E0.B8.94.E0.B8.81.E0.B8.B2.E0.B8.A3_permission_.E0.B8.82.E0.B8.AD.E0.B8.87_logs" href="http://wiki.memoball.info/"></a><br />
<h3> <span class="mw-headline">จัดการ permission ของ logs</span></h3>
<pre>
# prevent access to PHP error log
&lt;Files PHP_errors.log&gt;
 Order allow,deny
 Deny from all
 Satisfy All
&lt;/Files&gt;
</pre>
<p><!--<br />
NewPP limit report<br />
Preprocessor node count: 13/1000000<br />
Post-expand include size: 0/2097152 bytes<br />
Template argument size: 0/2097152 bytes<br />
Expensive parser function count: 0/100<br />
--></p>
<p><!-- Saved in parser cache with key memoball_wiki:pcache:idhash:37-0!1!0!!th!2!edit=0 and timestamp 20090825115815 --></p>
<div class="printfooter">
<h5>รับข้อมูลจาก (Reference) : </h5>
<p>จาก WikiBall : <a href="http://wiki.memoball.info/index.php/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87_PHP_Error_logs_%E0%B9%83%E0%B8%99_.htaccess">http://wiki.memoball.info/index.php/การสร้าง_PHP_Error_logs_ใน_.htaccess</a></div>
<div id='catlinks' class='catlinks'>
<div id="mw-normal-catlinks"><a href="http://wiki.memoball.info//index.php/%E0%B8%9E%E0%B8%B4%E0%B9%80%E0%B8%A8%E0%B8%A9:Categories" title="พิเศษ:Categories">หมวดหมู่</a>:&#32;<span dir='ltr'><a href="http://wiki.memoball.info//index.php?title=%E0%B8%AB%E0%B8%A1%E0%B8%A7%E0%B8%94%E0%B8%AB%E0%B8%A1%E0%B8%B9%E0%B9%88:Linux&amp;action=edit&amp;redlink=1" class="new" title="หมวดหมู่:Linux (ยังไม่ได้สร้าง)">Linux</a></span> | <span dir='ltr'><a href="http://wiki.memoball.info//index.php?title=%E0%B8%AB%E0%B8%A1%E0%B8%A7%E0%B8%94%E0%B8%AB%E0%B8%A1%E0%B8%B9%E0%B9%88:PHP&amp;action=edit&amp;redlink=1" class="new" title="หมวดหมู่:PHP (ยังไม่ได้สร้าง)">PHP</a></span></div>
</div>
<p>			<!-- end content --></p>
<div class="visualClear"></div>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.memoball.info/03/187/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
