Created
November 13, 2017 09:25
-
-
Save alexandrugheorghe/83fb3f2d7817f1d7ff85eb474fb8c83d to your computer and use it in GitHub Desktop.
PHP Multiprocess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit','512M'); | |
CONST WORKER_POOL = 3; | |
CONST INPUT_FILE = 'in.txt'; | |
CONST OUTPUT_FILE = 'out.txt'; | |
CONST PROCESS_DESCRIPTORS = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['file', 'error.log', 'a']]; | |
$workers = []; | |
$pipes = []; | |
$inputFile = fopen(INPUT_FILE, 'r'); | |
stream_set_blocking($inputFile, false); | |
$lineNumber = 0; | |
for ($index = 0; $index < WORKER_POOL; $index++) { | |
$pipes[$index] = []; | |
$workers[] = proc_open('php worker.php', PROCESS_DESCRIPTORS, $pipes[$index]); | |
} | |
while(!feof($inputFile)) { | |
$line = fgets($inputFile); | |
fwrite($pipes[$lineNumber % WORKER_POOL][0], $line); | |
$lineNumber++; | |
} | |
fclose($inputFile); | |
for ($index = 0; $index < WORKER_POOL; $index++) { | |
fclose($pipes[$index][0]); | |
} | |
$outputFile = fopen(OUTPUT_FILE, 'w'); | |
stream_set_blocking($outputFile, false); | |
$lineNumber = 0; | |
while (!feof($pipes[$lineNumber % WORKER_POOL][1])) { | |
$result = fgets($pipes[$lineNumber % WORKER_POOL][1]); | |
fputs($outputFile, $result); | |
$lineNumber++; | |
} | |
fclose($outputFile); | |
for ($index = 0; $index < WORKER_POOL; $index++) { | |
fclose($pipes[$index][1]); | |
} | |
for($index = 0; $index < WORKER_POOL; $index++) { | |
proc_close($workers[$index]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3388,-4200,2042,-3204 | |
2456,-894,594,549 | |
-3765,-146,-968,2616 | |
1188,-1772,3479,-4172 | |
1934,-4371,-488,1721 | |
554,623,1720,-4633 | |
-1613,-3469,301,-2528 | |
3966,1576,2185,4581 | |
-2621,-3078,-4622,2417 | |
-1389,4472,-1285,-820 | |
2091,3747,3154,-1553 | |
3284,3728,4825,-3244 | |
1319,170,-1078,-1469 | |
-3263,4695,4641,-4579 | |
-3436,-3944,4466,-1294 | |
-221,3980,-449,-3068 | |
1405,34,-3125,2053 | |
1583,3739,2403,-1706 | |
-3138,1898,-1617,988 | |
4574,813,3396,-3294 | |
3344,-2274,1581,-1766 | |
193,-1344,2938,817 | |
-373,-1177,-3421,-4162 | |
-4645,2099,1869,-4788 | |
-4249,-1111,-4596,-1210 | |
-3567,2990,3499,-2116 | |
3404,-3959,4769,-1588 | |
2832,-2985,-864,4092 | |
-3643,-1170,-1515,3340 | |
343,-446,1144,3444 | |
-736,-1237,-4073,2005 | |
-3065,981,-968,-2744 | |
1369,1704,2229,-3305 | |
3635,1589,-4265,-3433 | |
-682,-1504,-304,-607 | |
-3662,4475,4230,-998 | |
1367,-36,-3382,-1596 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit','512M'); | |
$input = fopen('php://stdin', 'r'); | |
while (!feof($input)) { | |
$line = fgets($input); | |
if (strlen($line) > 0) { | |
$numbers = explode(',', preg_replace('~[\r\n]+~', '', $line)); | |
$result = round(sqrt(pow(/**/(int)$numbers[2] - (int)$numbers[0], 2) + pow((int)$numbers[3] - (int)$numbers[1],2)), 2); | |
echo $result . "\n"; | |
} | |
} | |
fclose($input); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment