Home> php教程> PHP源码> body text

Write files on the hard disk under a certain amount of concurrency

大家讲道理
Release: 2016-11-09 10:11:55
Original
1270 people have browsed it

= $max_size) { $new_log_file = '/logs/error_' . date('YmdHis') . '.log'; rename($log_file, $new_log_file); } } $fp=fopen($log_file,'a+'); if($fp){ $startTime=microtime(); do{ // 这个循环可以保证进程在尝试1m后,如果未能锁定文件,则放弃写入日志的操作 $canWrite=flock($fp,LOCK_EX); if(!$canWrite){ usleep(round(rand(0,100)*1000)); } }while((!$canWrite)&&((microtime()-$startTime)<1000)); if($canWrite){ $content = date('Y-m-d H:i:s') . ' ' . $log_content . "\r\n"; fwrite($fp,$content); } fclose($fp); } }
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!