PHP 会话 线程 进程,php进程后台调用(多线程/进程)
/* Note that the call itself isn‘t sanitized in any way, so it‘s
important to make sure that this can‘t be exploited, see
docs for escapeshellcmd() for details
*/
// Demonstration
if(launchBackgroundProcess(‘touch testfile.txt‘)){
print ‘Successfully launched background process‘;
}
/**
* Launch Background Process
*
* Launches a background process (note, provides no security itself, $call must be sanitized prior to use)
* @param string $call the system call to make
* @author raccettura
*/
function launchBackgroundProcess($call) {
// Windows
if(is_windows()){
pclose(popen(‘start /b ‘.$call.‘‘, ‘r‘));
}
// Some sort of UNIX
else {
pclose(popen($call.‘ /dev/null &‘, ‘r‘));
}
return true;
}
/**
* Is Windows
*
* Tells if we are running on Windows Platform
* @author raccettura
*/
function is_windows(){
if(PHP_OS == ‘WINNT‘ || PHP_OS == ‘WIN32‘){
return true;
}
return false;
}
?>
關鍵在于
‘ /dev/null &‘
(*nix下的后臺運行方式)
和
‘start /b ‘
(windows下的后臺運行方式)
想了一下,似乎很多腳本都可以用這種方式將一些操作移交后臺進行,可以大幅提高效率。
如果要返回結果,則可以用異步執行并返回狀態的方式,邊處理邊顯示處理結果,這可以用在一些耗時較長的操作上,比如論壇備份\恢復等:
/****
* Again notice this is unsanitized since we trust ourselves. Coming from the web
* it would need to be sanitized to ensure it‘s safe to use. see escapeshellarg()
****/
$hostname = ‘accettura.com‘;
?>
Traceroute to $hostnameif(is_windows()){
$cmd = ‘tracert -w 10‘;
} else {
$cmd = ‘traceroute -w 10‘;
}
$handle = popen("$cmd $hostname 2>&1", ‘r‘);
while(!feof($handle)) {
$buffer = fgets($handle);
$buffer = ‘
‘.trim(htmlspecialchars($buffer)).‘
‘;echo $buffer;
ob_flush();
flush();
}
pclose($handle);
/**
* Is Windows
*
* Tells if we are running on Windows Platform
* @author raccettura
*/
function is_windows(){
if(PHP_OS == ‘WINNT‘ || PHP_OS == ‘WIN32‘){
return true;
}
return false;
}
?>
繼續php多線程/進程的問題
昨天找到了進程后臺運行的方法,今天測試了一下,發現popen的速度很慢,要40-50毫秒,exec更慢!類似的程序調用命令,都要經過系統調用,每次都開啟一個php進程想必很慢。
比較笨的辦法還是用fsockopen去通過http在server端get,試了一下,這樣不會慢,缺點是增加了apache負載,每個請求都要在后臺再請求一次。
我寫了段腳本test.php,用fsockopen循環連接本地另外一個腳本test1.php,不做任何操作立即關閉連接,test1.php每次在文本文件test.cache中寫入一行,循環100次的時候執行很快,test.cache中也正確的記錄了100行。當循環1000次的時候,問題就來了,test.php執行了21.6888360977秒,也就是21秒內向apache發了1000個請求連test1.php,系統馬上沒有響應了,內存占用飆升到1G多,一分鐘之后才恢復正常。test.cache中丟失63行,可能是由于apache超載造成的,但是系統內存卻始終沒有降下來,apache的占用了83M,剩下的不知道怎么回事- -
最后又找了半天,找到了fork實現的真正多線程!fork是pcntl(Process Control Functions)下的一個函數,pcntl只支持*nix系統,目前沒有windows下的相關模塊。(文檔中說需要在編譯php時--enable-pcntl,我用phpize編譯成php模塊的方式,通過在php.ini中添加extension=pcntl.so也可以使用。)
php手冊里面就有了,但是在網上幾乎找不到中文的文檔!文檔里面有這樣一個實例:
declare(ticks=1);
echo "I‘m going to be a Dad.n";
if (spawn_child(‘child_function‘)) {
echo ‘Parent pid:‘.posix_getpid()."n";
}
echo "I‘m going to be a Dad again!.n";
if (spawn_child(‘child_function‘,1,2,3,4)) {
echo ‘Child - 2 Parent pid:‘.posix_getpid()."n";
}
echo "What you‘re pregnant again!?.n";
if (spawn_child(‘grand_children‘)) {
echo ‘Child - 3 Parent pid:‘.posix_getpid()."n";
}
function grand_children() {
echo "Dad I‘m going to have a baby.n";
spawn_child(‘child_function‘,‘Joe‘);
echo "I‘m so proud of my kids.n";
}
function spawn_child($function) {
$original_pid = posix_getpid();
$pid = pcntl_fork();
if ($pid == -1) {
die ("Unable to spawn childn");
}
$new_pid = posix_getpid();
if ($new_pid == $original_pid) {
return true;
}
if (function_exists($function)) {
$numargs = func_num_args();
if ($numargs > 1) {
$args = func_get_args();
//print_r ($args);
unset($args[0]);
call_user_func($function,$args);
} else {
call_user_func($function);
}
echo "Done with child ".$new_pid." they moved out.n";
exit;
} else {
die ("$function does not existn");
}
}
function child_function() {
echo ‘Child pid:‘.posix_getpid()."n";
$args = func_get_args();
if (!empty($args))
print_r ($args);
}
pcntl_wait( $status);
?>
用pcntl實現的多線程可以解決很多問題了,但是似乎還是不能解決我的問題。在多線程雖然可以讓程序并行執行,但所有的程序仍然在前臺完成,在所有的線程完成之前,瀏覽器仍然會顯示載入狀態。在這種狀態下,javascript代碼不會執行,而我正是需要php產生一段javascript腳本,我想我的目的可能不是多線程或者并發處理,而是異步調用、后臺執行,讓生成腳本之外的操作在后臺執行。
所以我的這種情況用最開始的fsockopen()方法可能更有效,但是fsockopen是不能進行異步處理的,如果要跟打開的連接進行交互,就沒有任何優勢可言了。不過我在網上找到了可以進行異步通信的辦法,其實是變相的實現了多線程:
總結
以上是生活随笔為你收集整理的PHP 会话 线程 进程,php进程后台调用(多线程/进程)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机将取代老师吗 英语作文,大学英语作
- 下一篇: 虚拟机非正常关闭 无法打开