1

READ FURTHER BELOW at CLI, FOR THE CLI QUESTION, WHICH JUST ADDED TO THE CONVERSATION! THX!


I have written a script which processes an xml file of around 160'000 entries with 48.1MB and a text file of 150'000 entries with 31.1MB, including some directory searches for external files, heavy interlinking and recursive checks and the result formatted and all saved into html files.

Surely, I did review the program couple times and ended up with the most efficient code I could think of. This is a local program and the generator doesn't need to run regularly. One could argue that I should use an other language than PHP, but PHP with simplexml, etc. just works the best for me and for this purpose. Also a set_time_limit('70000') doesn't bother me.

Although, here my question, is it possible to make the apache2 on my linux system, use my 4 CPU cores running my PHP script? Even if I split the process and make several request's simultaneously, the CPU usage can't go above 1 CPU at a time.

I googled this topic but couldn't find a solution, so I may have to just run it over night, even though, I would appreciate some help to boost that thing!!!

ADDED INFO - And here a picture of my processes: enter image description here

CLI: I need to call my index.php in the linux terminal to execute. But I also wanna send four post variables ($_POST['example']) to the script. On top of that, I am looking for having my echos presented in some output file. Could anyone help quickly with the terminal command and the php command to track those 4 post variables inside:

if (PHP_SAPI === 'cli')
{
   // ...
} 

? ...sorry but this is my first php-cli interaction. Thx!

4

4 回答 4

1

不,单个 PHP 脚本永远不会使用多个线程,因此始终在单核上运行。

根据您所做的事情有多少相互依赖,无论如何您都无法轻松地将它们拆分到多个线程上。

于 2012-05-24T07:11:55.973 回答
1

编辑:作者的回应

这不是一个解决方案,而是一个很好的解决方法。我使用 linux/apache2 安装克隆我的虚拟机以启动相同的进程,但文件/进程的不同部分在不同的 vm 上,这让主机系统为每个虚拟系统应用一个核心,这样我就可以分解进程时间大约是 4 倍。感谢您的帖子!

================

如果它是本地的,并且您想不时运行它,您可能应该从cron作业中调用它。这样,您可以为您正在执行的每项任务生成一个进程。如果您确实想使用 PHP,您甚至可以从 cron 行调用 PHP 来执行此操作。

尽管如此,听起来你无论如何都在做一个固有的单线程进程,如果你想要它更快,可能应该使用不是 PHP 的东西。

于 2012-05-24T07:16:53.643 回答
1

也许你可以使用 Spork!这是一个 php 库,允许您将 php 进程分叉为多个进程。

<?php

use Spork\Deferred\DeferredFactory;
use Spork\ProcessManager;

$manager = new ProcessManager(new DeferredFactory());
$manager->fork(function() {
    // do something in another process!
})->then(function($output, $status) {
    // do something in the parent process when it's done!
});

https://github.com/kriswallsmith/spork

于 2012-05-24T08:21:21.013 回答
0

解决方案,感谢 ThiefMaster 和 Zebediah49 推荐 cli 和通过链接支持我的朋友:http: //ch.php.net/manual/en/reserved.variables.argv.php / http://ch.php.net /manual/en/function.getopt.php

在这里我如何通过cli调用php:

//whenRunFromCLI
//callCLI
//php index.php './data/xyfullFile1.xml' './data/xxfullFile2.utf' 0 60000
//php index.php './data/xyfullFile1.xml' './data/xxfullFile2.utf' 60000 120000
//php index.php './data/xyfullFile1.xml' './data/xxfullFile2.utf' 120000 all
if (PHP_SAPI === 'cli'){
    $_POST['xml'] = $argv[1];
    $_POST['example'] = $argv[2];
    @$_POST['rangeFrom'] = $argv[3];
    @$_POST['rangeTo'] = $argv[4];
}

以及在三个终端调用php文件的结果: 在此处输入图像描述

我知道,我必须为我的虚拟机提供更多内存,幸运的是我还有 8GB 的​​空闲空间;-)

欢呼与和平!

于 2012-05-25T01:25:15.767 回答