php PHP多线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1532065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php multithread
提问by Roch
Php, isn't really made for multithread but do you have any workarround to deal with threads in php.
Php,并不是真正为多线程设计的,但是你有任何解决方法来处理 php 中的线程吗?
回答by Adam Wright
There are a few solutions, varying from "Hmmm, just about OK" through to "Poke out your eyes".
有几种解决方案,从“嗯,差不多可以”到“戳出你的眼睛”不等。
- Write your multithreaded code as a PHP extension. Probably the most supported, but you need to write in C (or another language supported for extensions).
- Spawn child processes onto the underlying OS, and read/write to their input/output with standard file handles. See popenfor one route in, or PCNTL. Reasonable, and you can use PHP from the command line.
- Make other HTTP requests to yourself via CURLor similar, thus piggybacking on your web servers multi-processing capacity. Keeps all your code "web like", but runs the risk of irate support developers tracking you down and breaking thumbs.
回答by GHayes
Since this is a top result on google, php has a new extension, pthreads: http://www.php.net/manual/en/book.pthreads.phpspecifically for this.
由于这是 google 上的最佳结果,因此 php 有一个新的扩展名 pthreads:http: //www.php.net/manual/en/book.pthreads.php专门为此。
回答by rogeriopvl
Check the PCNTL library. It may help you to emulate some thread behavior.
检查PCNTL 库。它可以帮助您模拟一些线程行为。
Also there's this class:
还有这个类:
"This class can emulate the execution of program threads using separate HTTP requests to the same script.
It establishes an HTTP connection to the same Web server to execute the same PHP script. It sends a request passing the name a function to execute and an argument to be passed to that function.
The requested script executes some code that detects the thread execution request and calls the specified function.
When the thread request script ends, the return values of the called function is returned as a serialized string.
The calling script can execute other tasks while the thread script runs. The results may be collected later when the thread script ends."
“该类可以使用对同一脚本的单独 HTTP 请求来模拟程序线程的执行。
它建立到同一个 Web 服务器的 HTTP 连接以执行同一个 PHP 脚本。它发送一个请求,传递名称要执行的函数和要传递给该函数的参数。
请求的脚本执行一些检测线程执行请求并调用指定函数的代码。
当线程请求脚本结束时,被调用函数的返回值作为序列化字符串返回。
调用脚本可以在线程脚本运行时执行其他任务。稍后可能会在线程脚本结束时收集结果。”
回答by Vinko Vrsalovic
You can also use processes instead of threads using pipes or sockets to communicate them.
您还可以使用进程而不是线程使用管道或套接字来进行通信。
回答by Pir Abdul
Multithreading means performing multiple tasks or processes simultaneously, we can achieve this in php by using following code,although there is no direct way to achieve multithreading in php but we can achieve almost same results by following way.
多线程意味着同时执行多个任务或进程,我们可以在 php 中使用以下代码来实现这一点,虽然在 php 中没有直接的方法来实现多线程,但我们可以通过以下方式实现几乎相同的结果。
chdir(dirname(__FILE__)); //if you want to run this file as cron job
for ($i = 0; $i < 2; $i += 1){
exec("php test_1.php $i > test.txt &");
//this will execute test_1.php and will leave this process executing in the background and will go
//to next iteration of the loop immediately without waiting the completion of the script in the
//test_1.php , $i is passed as argument .
}
}
Test_1.php
测试_1.php
$conn=mysql_connect($host,$user,$pass);
$db=mysql_select_db($db);
$i = $argv[1]; //this is the argument passed from index.php file
for($j = 0;$j<5000; $j ++)
{
mysql_query("insert into test set
id='$i',
comment='test',
datetime=NOW() ");
}
This will execute test_1.php two times simultaneously and both process will run in the background simultaneously ,so in this way you can achieve multithreading in php.
这将同时执行 test_1.php 两次,并且两个进程将同时在后台运行,这样您就可以在 php 中实现多线程。
This guy done really good work Multithreading in php(Dead link)
这家伙在 php 中的多线程做得非常好 (死链接)
回答by Damir Olejar
Although not the best solution, using VirtualBox (which emulates all CPU cores as one) and then putting a PHP into an OS in VitualBox, has given me a lot of improvement in a performance while computing some big-data numbers (although you may lose the 64-bit performance and RAM). Nevertheless, it worked for what I wanted to accomplish.
尽管不是最好的解决方案,但使用 VirtualBox(将所有 CPU 内核模拟为一个内核)然后将 PHP 放入 VitualBox 中的操作系统中,在计算一些大数据数字时,性能有了很大的提高(尽管您可能会丢失64 位性能和 RAM)。尽管如此,它对我想要完成的事情有效。
The idea is that you have to emulate it.
这个想法是你必须模仿它。

