<?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>Werx Limited &#187; php</title>
	<atom:link href="http://werxltd.com/wp/category/software-development/php-software-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://werxltd.com/wp</link>
	<description>We make IT work.</description>
	<lastBuildDate>Wed, 08 Sep 2010 17:00:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Process forking and threading with PHP</title>
		<link>http://werxltd.com/wp/2010/08/23/process-forking-with-php/</link>
		<comments>http://werxltd.com/wp/2010/08/23/process-forking-with-php/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 12:00:49 +0000</pubDate>
		<dc:creator>wes</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[distributed processing]]></category>
		<category><![CDATA[parallel processing]]></category>
		<category><![CDATA[php threads]]></category>
		<category><![CDATA[pid]]></category>
		<category><![CDATA[process id]]></category>
		<category><![CDATA[serializing]]></category>
		<category><![CDATA[shared memory]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://werxltd.com/wp/?p=663</guid>
		<description><![CDATA[I&#8217;ve been working on a rather large web application which is responsible for combining data from a variety of sources and presenting the data to the end user in a clean, unified fashion. During this process we sometimes run into cases where multiple related calls are made, each to perform some transformative work on a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a rather large web application which is responsible for combining data from a variety of sources and presenting the data to the end user in a clean, unified fashion. During this process we sometimes run into cases where multiple related calls are made, each to perform some transformative work on a single set of data. We decided these calls could be made in a more parallel fashion and as such started looking into ways of parallelizing PHP so that relatively expensive operations could be performed at the same time and then the results combined in the end.</p>
<p>We examined a few possible solutions such as <a href="http://gearman.org/">Gearman</a>, <a href="http://webforumz.com/php/12595-multithreaded-php.htm">popen</a>, and <a href="http://www.developertutorials.com/tutorials/php/parallel-web-scraping-in-php-curl-multi-functions-375/">multi curl</a>. However all of these methods seemed to require more overhead than they were worth. What I really wanted to see was something more along the lines of <a href="http://en.wikipedia.org/wiki/POSIX_Threads">POSIX threads</a> to distribute the work load and <a href="http://en.wikipedia.org/wiki/Shared_memory">shared memory</a> for passing data between the parent and child threads.</p>
<p>After some searching through PHP extensions and the official documentation I ran across <a href="http://us3.php.net/manual/en/refs.fileprocess.process.php">PHP&#8217;s Process Control Extensions</a> suite which contains <a href="http://us3.php.net/manual/en/ref.pcntl.php">PCNTL functions</a>, one of which is <a href="http://us3.php.net/manual/en/function.pcntl-fork.php">pcntl_fork</a>. Combined with PHP&#8217;s <a href="http://us3.php.net/manual/en/ref.shmop.php">Shared Memory Functions</a>, this promises to fit the bill of inexpensive distribution of processing tasks along with low-overhead <a href="http://en.wikipedia.org/wiki/Inter-process_communication">inter process communication</a>.</p>
<p>Here is a sample proof-of-concept script. I&#8217;ll outline what it does below:</p>
<pre class="brush:php">$data = array();

echo "Parent PID: ".getmypid().PHP_EOL;

function forkTest(array &amp;$data) {
	$pids = array();

	$parent_pid = getmypid();

	for($i = 0; $i &lt; 10; $i++) {
		if(getmypid() == $parent_pid) {
			$pids[] = pcntl_fork();
			echo "Forking child, \$pids now has ".count($pids)." elements".PHP_EOL;
		}
	} 	

	if (getmypid() == $parent_pid) {
		/* Parent thread */
		echo "Hello from parent: ".getmypid().PHP_EOL;
		array_push($data, "parent".getmypid()); 		  		 

		/* Process childrens' results as they exit */
		while(count($pids) &gt; 0) {
			$pid = pcntl_waitpid(-1, $status);
			echo "Attempting to open memory with pid: ".$pid.PHP_EOL;
			$shm_id = shmop_open($pid, "a", 0, 0);

			$shm_data = unserialize(shmop_read($shm_id, 0, shmop_size($shm_id)));
			shmop_delete($shm_id);
			shmop_close($shm_id);

			$data = array_merge($data, $shm_data);

			/* Hunt down and remove pid entry */
			foreach($pids as $key =&gt; $tpid) {
				if($pid == $tpid) unset($pids[$key]);
			}
		}

		echo "All children exited, \$data now has:".count($data)." elements".PHP_EOL;
		$pids = array();
	} else {
		/* Children threads */
		$pdata = array();
		echo "Hello from child: ".getmypid().PHP_EOL;
		array_push($pdata, "child".getmypid());
		$data_str = serialize($pdata);

		$shm_id = shmop_open(getmypid(), "c", 0644, strlen($data_str));
		if (!$shm_id) {
			echo "Couldn't create shared memory segment".PHP_EOL;
		} else {
			if(shmop_write($shm_id, $data_str, 0) != strlen($data_str)) {
				echo "Couldn't write shared memory data".PHP_EOL;
			}
		}

		sleep(rand(1,10));
		exit(0);
	}
}

/* Run the test 10 times */
for($f = 0; $f &lt; 10; $f++) {
	echo "Running $f forkTest()".PHP_EOL;
	forkTest($data);
}

echo "Fork test finished, \$data now contains ".count($data)." elements".PHP_EOL;
echo "\$data:".PHP_EOL.json_encode($data);</pre>
<p>This code describes a function that spawns 10 child worker threads, each of which gets a reference to the global $data array. Each child thread pushes a string element containing the child thread&#8217;s <a href="http://en.wikipedia.org/wiki/Process_identifier">process identifier</a> into the array, serializes it, and then places it into a shared memory slot with the process id serving as the shared memory id. The parent process waits for each child thread to exit, gathers the data from shared memory, clears the shared memory, and then combines the results into the master $data array. My test application runs through this function 10 times to demonstrate how <a href="http://www.electrictoolbox.com/article/php/process-forking/">forking in PHP</a> can be safe and memory efficient. The result should be a $data array with 110 elements in it. I&#8217;ve thrown in sleep commands with a random time between 1 and 10 seconds to show how threads can return at different times.</p>
<p>No doubt optimizations can be made but this should serve as at least a rudimentary example of true and efficient threading in PHP. Well, provided that the work you are planning on doing is worth the overhead (which, small as it may be, still exists and should be factored in) and provided that you do not mind locking your application down to a POSIX environment (meaning the above code will not work on windows platforms).</p>
]]></content:encoded>
			<wfw:commentRss>http://werxltd.com/wp/2010/08/23/process-forking-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using PHP to write PHP</title>
		<link>http://werxltd.com/wp/2010/07/29/using-php-to-write-php/</link>
		<comments>http://werxltd.com/wp/2010/07/29/using-php-to-write-php/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 19:00:49 +0000</pubDate>
		<dc:creator>wes</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[kml]]></category>
		<category><![CDATA[mapping]]></category>
		<category><![CDATA[utilities]]></category>
		<category><![CDATA[wkt]]></category>

		<guid isPermaLink="false">http://werxltd.com/wp/?p=630</guid>
		<description><![CDATA[I ran into a situation recently where I had a very large array object that was not going to change. It was the polygon coordinates for the world countries to be used in KML provided by Thematic Mapping. I didn&#8217;t wan&#8217;t to pull this data out of MySQL every time I went to build a [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a situation recently where I had a very large array object that was not going to change. It was the polygon coordinates for the world countries to be used in <a href="http://code.google.com/apis/kml/documentation/">KML</a> provided by <a href="http://thematicmapping.org/">Thematic Mapping</a>.</p>
<p>I didn&#8217;t wan&#8217;t to pull this data out of MySQL every time I went to build a map, I wanted to use one giant static array instead. So to write the array back out in a re-usable fashion I developed the following code which can take any array, of any depth, associative or index based, and spit out corresponding PHP that you can copy and paste back into your script.</p>
<pre class="brush:php">function phpPrintArr(array $arr) {
	echo "array(".PHP_EOL;

	$c = 1;
	$total = count($arr);

	foreach($arr as $i =&gt;$v) {
		$iq = is_numeric($i) ? '' : '"';
		$vq = is_numeric($v) ? '' : '"';
		$e = $c &lt; $total ? ',' : '';

		if(is_array($v)) {
			echo $iq.$i.$iq.'=&gt;'.PHP_EOL;
			phpPrintArr($v);
			echo $e.PHP_EOL;
		} else {
			echo $iq.$i.$iq.'=&gt;'.$vq.$v.$vq.$e.PHP_EOL;
		}
		$c++;
	}

	echo ")".PHP_EOL;
}</pre>
<p>Oh, and for anyone who is interested in the array of country polygons this produced from the Thematic Mapping Engine data, <a href="http://werxltd.com/software/TME_Countries">here is the rather large array</a>. Feel free to use it in your own KML project.</p>
]]></content:encoded>
			<wfw:commentRss>http://werxltd.com/wp/2010/07/29/using-php-to-write-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HipHop for PHP</title>
		<link>http://werxltd.com/wp/2010/04/29/hiphop-for-php/</link>
		<comments>http://werxltd.com/wp/2010/04/29/hiphop-for-php/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 12:00:15 +0000</pubDate>
		<dc:creator>wes</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[it industry]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[high performance]]></category>
		<category><![CDATA[hiphop]]></category>
		<category><![CDATA[web scale]]></category>

		<guid isPermaLink="false">http://werxltd.com/wp/?p=573</guid>
		<description><![CDATA[Earlier this year Facebook developers caused quite a stir in the PHP community by releasing HipHop, the central piece in their high-performance arsenal. Here is the video of the initial announcement along with some juicy technical details: If you are interested in seeing how your PHP project fares when run through HipHop, check out the [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this year <a href="http://developers.facebook.com/blog/post/358">Facebook developers</a> caused quite a stir in the PHP community by releasing <a href="http://en.wikipedia.org/wiki/HipHop_(software)">HipHop</a>, the central piece in their high-performance arsenal.</p>
<p><a href="http://www.ustream.tv/recorded/4409735">Here is the video</a> of the initial announcement along with some juicy technical details:<br />
<object id="utv946196" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="386" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="name" value="utv_n_687056" /><param name="flashvars" value="loc=%2F&amp;autoplay=false&amp;vid=4409735&amp;locale=en_US" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.ustream.tv/flash/video/4409735" /><embed id="utv946196" type="application/x-shockwave-flash" width="480" height="386" src="http://www.ustream.tv/flash/video/4409735" allowscriptaccess="always" allowfullscreen="true" flashvars="loc=%2F&amp;autoplay=false&amp;vid=4409735&amp;locale=en_US" name="utv_n_687056"></embed></object></p>
<p>If you are interested in seeing how your PHP project fares when run through HipHop, check out the official <a href="http://github.com/facebook/hiphop-php">HipHop project page</a>.</p>
<p>With tools like HipHop, scripting languages like PHP are no longer subject to the charges of gross computational inefficiency. In short, it&#8217;s a great day to be a web developer.</p>
]]></content:encoded>
			<wfw:commentRss>http://werxltd.com/wp/2010/04/29/hiphop-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP-QREncoder WordPress Plugin</title>
		<link>http://werxltd.com/wp/2010/03/04/wp-qrencoder-wordpress-plugin/</link>
		<comments>http://werxltd.com/wp/2010/03/04/wp-qrencoder-wordpress-plugin/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 20:00:31 +0000</pubDate>
		<dc:creator>wes</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[qrencoder]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp-qrencoder]]></category>

		<guid isPermaLink="false">http://werxltd.com/wp/?p=507</guid>
		<description><![CDATA[I managed to get an Android powered phone recently and quickly discovered the Barcode Scanner app is a common and seemingly preferred method of encoding and transmitting data in the Android community (and others as well I&#8217;m sure). In less than a week I&#8217;ve grown to love the Android platform and I&#8217;ve already got a [...]]]></description>
			<content:encoded><![CDATA[<p>I managed to get an <a href="http://www.android.com/">Android</a> powered phone recently and quickly discovered the <a href="http://www.androidtapp.com/barcode-scanner/">Barcode Scanner</a> app is a common and seemingly preferred method of encoding and transmitting data in the Android community (and others as well I&#8217;m sure).</p>
<p>In less than a week I&#8217;ve grown to love the Android platform and I&#8217;ve already got a few ideas for some Android apps to write. But first I wanted to make sure I could post links to my apps using handy-dandy QR-encoded images easily within WordPress.</p>
<p>So, borrowing somewhat from the WP-Footnotes plugin I set about to create the first rendition of the <a href="http://werxltd.com/wp/portfolio/wordpress-qrencoder-plugin/">WP-QREncoder</a> plugin for WordPress.</p>
<p>This plugin is capable of encoding any string of text (specific use case is a URL) and is still in it&#8217;s infancy so I would appreciate any feedback you might have.</p>
<p><a href="http://werxltd.com/wp/wp-content/uploads/wp-qrencoder.tar.gz">Download WP-QREncoder plugin here.</a></p>
<p>For more information (including usage) see the <a href="http://werxltd.com/wp/portfolio/wordpress-qrencoder-plugin/">plugin&#8217;s permanent page here</a>.</p>
<p>Oh, and here&#8217;s an example of the plugin in action:</p>
<p><img src="http://werxltd.com/wp/wp-content/plugins/wp-qrencoder/qr_img.php?d=http://www.werxltd.com&#038;t=other&#038;s=4&#038;s=M" /></p>
]]></content:encoded>
			<wfw:commentRss>http://werxltd.com/wp/2010/03/04/wp-qrencoder-wordpress-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
