<?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>多进程 &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/%E5%A4%9A%E8%BF%9B%E7%A8%8B/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Wed, 24 Oct 2012 16:48:55 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>
	<item>
		<title>PHP多进程写入文件</title>
		<link>https://www.mudbest.com/php%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6/</link>
					<comments>https://www.mudbest.com/php%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6/#comments</comments>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Thu, 11 Oct 2012 07:37:41 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[加锁]]></category>
		<category><![CDATA[多进程]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1556</guid>

					<description><![CDATA[当多个进程需要对文件进行操作时，为了避免其中出现的种种问题，可对文件进行一个列队的方式处理，当处理完一个进程后 ... <a title="PHP多进程写入文件" class="read-more" href="https://www.mudbest.com/php%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6/" aria-label="阅读 PHP多进程写入文件">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>当多个进程需要对文件进行操作时，为了避免其中出现的种种问题，可对文件进行一个列队的方式处理，当处理完一个进程后，第二个进程继续……</p>
<p>用到的系统函数有：file_exists(),usleep(),file_put_contents(),fopen(),fwrite(),fclose(),unlink()。</p>
<p><strong>file_exists() 检查文件或目录是否存在</strong><span id="more-1556"></span></p>
<pre class="brush: php; title: ; notranslate">
//bool file_exists ( string $filename )
//如果由 filename 指定的文件或目录存在则返回 TRUE，否则返回 FALSE。 
//在 Windows 中要用 //computername/share/filename 或者 \\computername\share\filename 来检查网络中的共享文件。
</pre>
<p>如果由 filename 指定的文件或目录存在则返回 TRUE，否则返回 FALSE。<br />
Example #1 测试一个文件是否存在</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    echo &quot;The file $filename exists&quot;;
} else {
    echo &quot;The file $filename does not exist&quot;;
}
?&gt; 
</pre>
<p><strong>usleep() 在几微秒的延迟执行</strong></p>
<pre class="brush: php; title: ; notranslate">
//void usleep ( int $micro_seconds )
//延迟程序执行给定数量的微秒。
</pre>
<p>Example #1 usleep() example</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
// Current time
echo date('h:i:s') . &quot;\n&quot;;
// wait for 2 seconds
usleep(2000000);
// back!
echo date('h:i:s') . &quot;\n&quot;;
//结果
//11:13:28
//11:13:30
?&gt; 
</pre>
<p><strong>file_put_contents() 将一个字符串写入文件</strong></p>
<pre class="brush: php; title: ; notranslate">
int file_put_contents ( string $filename , string $data &#x5B;, int $flags &#x5B;, resource $context ]] )
/*和依次调用 fopen()，fwrite()以及 fclose() 功能一样。 
参数 data 可以是数组（但不能为多维数组），这就相当于 file_put_contents($filename, join('', $array))
自 PHP 5.1.0 起，data参数也可以被指定为 stream 资源，这里 stream 中所保存的缓存数据将被写入到指定文件中，这种用法就相似于使用 stream_copy_to_stream() 函数。*/
//参数
/*
filename
要被写入数据的文件名。 
data
要写入的数据。类型可以是 string，array或者是 stream 资源（如上面所说的那样）。 
flags
flags 可以是 FILE_USE_INCLUDE_PATH，FILE_APPEND和／或 LOCK_EX（获得一个独占锁定），然而使用 FILE_USE_INCLUDE_PATH 时要特别谨慎。 
context
一个 context 资源。 
*/
</pre>
<p><strong>unlink() — 删除文件</strong></p>
<pre class="brush: php; title: ; notranslate">
//bool unlink ( string $filename )
//删除 filename。和 Unix C 的 unlink() 函数相似。成功时返回 TRUE， 或者在失败时返回 FALSE. 
//Note: 自 PHP 5.0.0 起 unlink() 也可以用于某些URL 封装协议。参考Supported Protocols and Wrappers 中的列表看哪些封装协议支持 unlink()。 
//Note: 在 PHP 5.0.0 中增加了 对上下文(Context)的支持。 有关 上下文(Context) 的说明参见 Stream 函数。
//目录删除函数 rmdir()。 
</pre>
<p><strong>代码：</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * 多进程写入文件
 */
function write_file($filename, $text) {
	$filename_lock = $filename . '.lock';
	while ( 1 ) {
		if (file_exists ( $filename_lock )) { // 判断加锁未见是否存在
			usleep ( 1000 ); // 如果存在则以为妙计算进行sleep睡眠
		} else { // .....
			file_put_contents ( $filename_lock ); // 生成文件 touch()的效率没file_put_contents()高
			$t = fopen ( $filename, 'a+' ); // 打开文件流
			fwrite ( $t, $text ); // 写入文件
			fclose ( $t ); // 关闭写入
			//exit (); //test...
			unlink ( $filename_lock ); // 删除加锁文件
			break; // 跳出循环
		}
	}
	if (file_exists ( $filename_lock )) {
		unlink ( $filename_lock ); // 删除锁定文件
	}
}
?&gt;
</pre>
<p>DEMO:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
// write_file('1.txt', '123123123');
?&gt;
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mudbest.com/php%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
