I have a huge text file. I want to write specific lines from db to the file at specific places. I wrote a php script for this it ran for 17 days. To reduce the time I re-write the program using Php Pthreads as multi threaded program. Now I have divided the file into 100 small files. Now 100 threads are manipulating the files separately. The program runs fine for about a day but the command line gives CLI has stopped working error.
Here are the specifics:
Windows 7 Professional Service Pack 1
Processor: Intel(R) Core(TM) i5
Ram: 4 GB
PHP Version: 5.5.12
Pthreads Version: 2.0.10
This is the windows log error:
Log Name: Application
Source: Application Error
Date: 7/8/2015 7:41:22 AM
Event ID: 1000
Task Category: (100)
Level: Error
Keywords: Classic
User: N/A
Computer: ICT-DB-Migr
Description:
Faulting application name: php.exe, version: 5.5.12.0, time stamp: 0x53613fb5
Faulting module name: php5ts.dll, version: 5.5.12.0, time stamp: 0x53614545
Exception code: 0xc0000005
Fault offset: 0x000000000000ad68
Faulting process id: 0x10ec
Faulting application start time: 0x01d0b889e4ff1462
Faulting application path: C:\wamp\bin\php\php5.5.12\php.exe
Faulting module path: C:\wamp\bin\php\php5.5.12\php5ts.dll
Report Id: a05581e5-2516-11e5-9cc2-a0481c83537b
This is my php program:
<?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]."
");
}
}
fclose($file);
$time_end = microtime(true);
$execution_time = ($time_end - $time_start)/60;
echo 'Total Execution Time: '.$execution_time.' Mins'.PHP_EOL;