Php cli已停止运行Php Pthreads程序
我有一个巨大的文本文件。我想在特定位置将特定行从db写入文件。我为此编写了一个php脚本,它运行了17天。为了减少时间,我使用PHPThreads作为多线程程序重新编写程序。现在我已经把这个文件分成了100个小文件。现在有100个线程分别处理这些文件。该程序正常运行约一天,但命令行显示CLI已停止工作错误 具体情况如下: Windows 7专业服务包1Php cli已停止运行Php Pthreads程序,php,multithreading,pthreads,command-line-interface,Php,Multithreading,Pthreads,Command Line Interface,我有一个巨大的文本文件。我想在特定位置将特定行从db写入文件。我为此编写了一个php脚本,它运行了17天。为了减少时间,我使用PHPThreads作为多线程程序重新编写程序。现在我已经把这个文件分成了100个小文件。现在有100个线程分别处理这些文件。该程序正常运行约一天,但命令行显示CLI已停止工作错误 具体情况如下: Windows 7专业服务包1 处理器:英特尔(R)核心(TM)i5 内存:4 GB PHP版本:5.5.12 Pthreads版本:2.0.10 这是windows日志错误:
处理器:英特尔(R)核心(TM)i5
内存:4 GB
PHP版本:5.5.12
Pthreads版本:2.0.10 这是windows日志错误: 日志名称:应用程序
来源:应用程序错误
日期:2015年7月8日上午7:41:22
事件ID:1000
任务类别:(100)
级别:错误
关键词:经典
用户:不适用
计算机:ICT DB Migr
说明:
出错应用程序名称:php.exe,版本:5.5.12.0,时间戳:0x53613fb5
故障模块名称:php5ts.dll,版本:5.5.12.0,时间戳:0x53614545
异常代码:0xc0000005
故障偏移量:0x000000000000ad68
出错进程id:0x10ec
故障应用程序启动时间:0x01d0b889e4ff1462
出错的应用程序路径:C:\wamp\bin\php\php5.5.12\php.exe
故障模块路径:C:\wamp\bin\php\php5.5.12\php5ts.dll
报告Id:a05581e5-2516-11e5-9cc2-a0481c83537b 这是我的php程序:
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
/**
* Author:
* URL:
*/
$time_start = microtime(true);
class ThreadSafe extends Stackable {
public function run(){}
}
class WorkerThreads extends Thread
{
private $workerId;
private $conn;
public function __construct($id,$dataArray)
{
$this->workerId = $id;
$this->dataArray2 = $dataArray;
}
public function run()
{
include "db_connect.php";
$file_name="breaked_mark_files\USMARK_".$this->workerId.".mrk";
$file=fopen($file_name,"r") or exit("Unable to open file!");
$id=0;
$dataCount=-1;
$count852=0;
$dataSubCount=0;
while(!feof($file)){
echo 'worker '.$this->workerId." executing line record ".$id." line number".$dataCount."".PHP_EOL;
$line = fgets($file);
$field = substr($line,0,4);
$strlenth = strlen($line);
$data = substr($line,6);
/* if($dataCount==1){
//var_dump($dataArray2);
break;
} */
if($field == "=LDR"){
$count852=0;
$dataCount++;
$dataSubCoun=0;
$this->dataArray2[]=$line;
$dataSubCount++;
}elseif($field=="=852"){
if($count852 == 0){
$array=explode("\$",$data);
foreach($array as $key=>$value){
if($key !=0){
$str = $value;
$strlength=strlen($str);
$first_string=substr($str,0,1);
if($first_string == "p"){
$id=substr($str,1);
}
}
}
$this->dataArray2[]=$line;
//$count995++;
$dataSubCount++;
}else{
$this->dataArray2[]=$line;
$dataSubCount++;
}
}else{
$this->dataArray2[]=$line;
$dataSubCount++;
}
//$id = intval($id);
$id = mysql_real_escape_string($id);
$sql = "SELECT languageData FROM data_table WHERE id='$id'";
if($id != 0){
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$this->dataArray2[]=$row["languageData"];
$dataSubCount++;
}
} else {
//echo "0 results of main record for id ".$id."".PHP_EOL;
}
$sql = "SELECT holdingData FROM holding_data WHERE record_id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$this->dataArray2[]=$row["holdingData"];
$dataSubCount++;
}
$id=0;
} else {
//echo "0 results of holding data for id ".$id."".PHP_EOL;
}
}
}
$conn->close();
}
}
// Worker pool
$workers = [];
$array=[];
foreach (range(0, 49) as $i){
$array[$i]= new ThreadSafe();
}
// Initialize and start the threads
foreach (range(0, 49) as $i) {
$workers[$i] = new WorkerThreads($i,$array[$i]);
$workers[$i]->start();
}
// Let the threads come back
foreach (range(0, 49) as $i) {
$workers[$i]->join();
}
$file=fopen("writtenFle.txt","w") or exit("Unable to open file!");
$array_count = count($array);
foreach(range(0, ($array_count-1)) as $i){
$array_count2 = count($array[$i]);
foreach(range(0, ($array_count2-1)) as $y){
fwrite($file,$array[$i][$y]."\r\n");
}
}
fclose($file);
$time_end = microtime(true);
$execution_time = ($time_end - $time_start)/60;
echo 'Total Execution Time: '.$execution_time.' Mins'.PHP_EOL;
启用可能有用。您的CPU最多有8个逻辑处理内核。你不应该创建更多的线程。如果您的应用程序是I/O受限的,那么事件循环可能更有益。(,)此外,还有一个收益递减点。在某些情况下,线程会妨碍彼此的性能。如果已经有2个线程出现这种情况,我不会感到惊讶,前提是您的文件都驻留在同一个设备上。