<?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>Sentido Web &#187; divide venceras</title>
	<atom:link href="http://sentidoweb.com/tag/divide-venceras/feed" rel="self" type="application/rss+xml" />
	<link>http://sentidoweb.com</link>
	<description>Desarrollo web, HTML, CSS, Javascript, PHP, MySQL</description>
	<lastBuildDate>Tue, 10 Apr 2012 01:02:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Programación en paralelo con PHP</title>
		<link>http://sentidoweb.com/2008/09/24/programacion-en-paralelo-con-php.php</link>
		<comments>http://sentidoweb.com/2008/09/24/programacion-en-paralelo-con-php.php#comments</comments>
		<pubDate>Wed, 24 Sep 2008 22:00:00 +0000</pubDate>
		<dc:creator>displaynone</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[divide venceras]]></category>
		<category><![CDATA[paralelo]]></category>

		<guid isPermaLink="false">http://sentidoweb.com/2008/09/24/programacion-en-paralelo-con-php.php</guid>
		<description><![CDATA[Realizar algunas tareas puede ser algo lento, por lo que a veces es necesario ejecutar procesos en paralelo para agilizar la ejecución de un script. PHP no permite programación concurrente, por lo cual hay que simularlo, y para ello es necesario la utilización de sockets. El método es sencillo, se crean dos sockets y se [...]]]></description>
			<content:encoded><![CDATA[<p>Realizar algunas tareas puede ser algo lento, por lo que a veces es necesario ejecutar procesos en paralelo para agilizar la ejecución de un script. PHP no permite programación concurrente, por lo cual hay que simularlo, y para ello es necesario la utilización de sockets.</p>
<p>El método es sencillo, se crean dos sockets y se comprueba que hayan acabado de ejecutarse:</p>
<pre><code><pre class="php"><span style="color: #808080; font-style: italic;">// Ejecuta un proceso en un socket</span>
<span style="color: #000000; font-weight: bold;">function</span> JobStartAsync<span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$server</span>, <span style="color: #3E6D8F;">$url</span>, <span style="color: #3E6D8F;">$port</span>=<span style="color: #cc66cc;">80</span>,<span style="color: #3E6D8F;">$conn_timeout</span>=<span style="color: #cc66cc;">30</span>, <span style="color: #3E6D8F;">$rw_timeout</span>=<span style="color: #cc66cc;">86400</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #3E6D8F;">$errno</span> = <span style="color: #ff0000;">''</span>;
<span style="color: #3E6D8F;">$errstr</span> = <span style="color: #ff0000;">''</span>;
<a href="http://www.php.net/set_time_limit"><span style="color: #000066;">set_time_limit</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #3E6D8F;">$fp</span> = <a href="http://www.php.net/fsockopen"><span style="color: #000066;">fsockopen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$server</span>, <span style="color: #3E6D8F;">$port</span>, <span style="color: #3E6D8F;">$errno</span>, <span style="color: #3E6D8F;">$errstr</span>, <span style="color: #3E6D8F;">$conn_timeout</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #723b00;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #3E6D8F;">$fp</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">"$errstr ($errno)&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #723b00;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #3E6D8F;">$out</span> = <span style="color: #ff0000;">"GET $url HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #3E6D8F;">$out</span> .= <span style="color: #ff0000;">"Host: $server<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #3E6D8F;">$out</span> .= <span style="color: #ff0000;">"Connection: Close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<a href="http://www.php.net/stream_set_blocking"><span style="color: #000066;">stream_set_blocking</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/stream_set_timeout"><span style="color: #000066;">stream_set_timeout</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp</span>, <span style="color: #3E6D8F;">$rw_timeout</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/fwrite"><span style="color: #000066;">fwrite</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp</span>, <span style="color: #3E6D8F;">$out</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #723b00;">return</span> <span style="color: #3E6D8F;">$fp</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// Devuelve falso si el socket est&Atilde;&iexcl; desconectado o un string (que puede ser vacio) si est&Atilde;&iexcl; conectado</span>
<span style="color: #000000; font-weight: bold;">function</span> JobPollAsync<span style="color: #66cc66;">&#40;</span>&amp;<span style="color: #3E6D8F;">$fp</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #723b00;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp</span> === <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #723b00;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #723b00;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/feof"><span style="color: #000066;">feof</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<a href="http://www.php.net/fclose"><span style="color: #000066;">fclose</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #3E6D8F;">$fp</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #723b00;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #723b00;">return</span> <a href="http://www.php.net/fread"><span style="color: #000066;">fread</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp</span>, <span style="color: #cc66cc;">10000</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// Ejecuci&Atilde;&sup3;n inicial</span>
<span style="color: #808080; font-style: italic;">// Se ejecutan dos procesos cualquiera j1 y j2</span>
<span style="color: #3E6D8F;">$fp1</span> = JobStartAsync<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'localhost'</span>,<span style="color: #ff0000;">'/jobs/j1.php'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #3E6D8F;">$fp2</span> = JobStartAsync<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'localhost'</span>,<span style="color: #ff0000;">'/jobs/j2.php'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #723b00;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<a href="http://www.php.net/sleep"><span style="color: #000066;">sleep</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #3E6D8F;">$r1</span> = JobPollAsync<span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #3E6D8F;">$r2</span> = JobPollAsync<span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$fp2</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #723b00;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #3E6D8F;">$r1</span> === <span style="color: #000000; font-weight: bold;">false</span> &amp;&amp; <span style="color: #3E6D8F;">$r2</span> === <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #723b00;">break</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">"&lt;b&gt;r1 = &lt;/b&gt;$r1&lt;br&gt;"</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">"&lt;b&gt;r2 = &lt;/b&gt;$r2&lt;hr&gt;"</span>;
<a href="http://www.php.net/flush"><span style="color: #000066;">flush</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; @<a href="http://www.php.net/ob_flush"><span style="color: #000066;">ob_flush</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">"&lt;h3&gt;Jobs Complete&lt;/h3&gt;"</span>;</pre></code></pre>
<p>El autor profundiza algo más y comenta también cómo se resolvería el método <a href="http://phplens.com/phpeverywhere/?q=node/view/255">divide y vencerás</a>.</p>
<p>Siempre hay que tener cuidado cuando se programa en paralelo ya que podemos tener problemas al acceder simultáneamente a un recurso compartido.</p>
<p><a href="http://phplens.com/phpeverywhere/?q=node/view/254">Easy Parallel Processing in PHP</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sentidoweb.com/2008/09/24/programacion-en-paralelo-con-php.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

