apache PHP 中的多线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2325667/
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
Multithreading in PHP
提问by Harun Baris Bulut
I am trying to create a multi threaded PHP application right now. I have read lots of paper that explains how to create multi threading. All of those examples are built on diving the processes on different worker PHP files. Actualy that is also what I am trying to do but there is a problem :)
我现在正在尝试创建一个多线程 PHP 应用程序。我已经阅读了很多解释如何创建多线程的论文。所有这些示例都建立在对不同工作 PHP 文件的进程进行深入分析的基础上。实际上这也是我想要做的,但是有一个问题:)
There are too many jobs even to divide in 30 seconds (which is the execution time limit)
作业太多了,连30秒都分不了(这是执行时间限制)
We are using multi server environment on local network to complete the processes as the processes do not linked to each other or shares the same memory. We just need to fire them up and let them work at an exact time. Each of the processes works for 0.5 secs but it has a possibility to work for 30 secs.
我们在本地网络上使用多服务器环境来完成进程,因为进程不相互链接或共享相同的内存。我们只需要启动它们并让它们在准确的时间工作。每个进程工作 0.5 秒,但它有可能工作 30 秒。
Most of the examples fires up the PHP's and waits for the results. But unfortunately in my situation I dont need to expect a result from the thread. I just need it to execute the command and write the result to its own database.
大多数示例都会启动 PHP 并等待结果。但不幸的是,在我的情况下,我不需要期待线程的结果。我只需要它来执行命令并将结果写入它自己的数据库。
How can I achieve to fire up the phps and wait for them to work for 10000 processes ?
我怎样才能启动 phps 并等待它们为 10000 个进程工作?
ADDITIONAL INFO: I know that PHP neither have multi threading feature nor is built for. But we have to create a way to use it for instance we can send request to http://server1/dothis.php?jobid=5but standart methods makes us wait for the result. If we can manage to send request to this server without waiting for result it would solve our problem I think or we will need completely different approach such as a process divider with c++ or qt.
附加信息:我知道 PHP 既没有多线程功能,也不是专为它构建的。但是我们必须创建一种使用它的方法,例如我们可以向http://server1/dothis.php?jobid=5发送请求,但是标准方法让我们等待结果。如果我们可以设法在不等待结果的情况下向该服务器发送请求,我认为这将解决我们的问题,否则我们将需要完全不同的方法,例如使用 c++ 或 qt 的进程分隔符。
采纳答案by dmp
As has been pointed out, php doesn't support multi threading. However, and as tomaszsobczak mentioned, there is a library which will let you create "threads" and leave them running, and reconnect to them through other scripts to check their status and so on, called "Gearman".
正如已经指出的那样,php 不支持多线程。然而,正如 tomaszsobczak 提到的,有一个库可以让你创建“线程”并让它们运行,并通过其他脚本重新连接到它们以检查它们的状态等等,称为“ Gearman”。
From the project homepage: "Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages.It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates."
来自项目主页:“Gearman 提供了一个通用的应用程序框架,可以将工作分给其他更适合完成工作的机器或进程。它允许您并行工作、负载平衡处理以及在语言之间调用函数“它可以用于各种应用程序,从高可用性网站到数据库复制事件的传输。换句话说,它是分布式处理如何通信的神经系统。”
Rasmus' blog has a great write up about it here: playing with gearmanand for your case, it might just be the solution, although I've not read any in depth test cases... Would be interested to know though, so if you end up using this, please report back!
Rasmus 的博客在这里写了一篇很棒的文章: 玩齿轮人和你的情况,这可能只是解决方案,虽然我没有读过任何深入的测试用例......不过我很想知道,所以如果你最终使用了这个,请报告!
回答by Pekka
As the comments say, multi-threading is not possible in PHP. But based on your comment:
正如评论所说,多线程在 PHP 中是不可能的。但根据您的评论:
If we can manage to send request to this server without waiting for result it would solve our problem I think
如果我们可以设法在不等待结果的情况下向该服务器发送请求,我认为这将解决我们的问题
You can start a PHP script to run in the background using exec(), redirecting the script's output somewhere else (e.g. /dev/null). I think that's the best you will get. From the manual:
您可以使用exec()启动一个 PHP 脚本在后台运行,将脚本的输出重定向到其他地方(例如/dev/null)。我认为这是你能得到的最好的。从手册:
Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
注意:如果一个程序是用这个函数启动的,为了让它在后台继续运行,程序的输出必须重定向到一个文件或另一个输出流。如果不这样做,将导致 PHP 挂起,直到程序执行结束。
There are several notes and pointers in the User Contributed Comments, for example this snippetthat allows background execution on both Windows and Linux platforms.
User Contributed Comments 中有几个注释和提示,例如这个片段允许在 Windows 和 Linux 平台上后台执行。
Of course, that PHP script will not share the state, or any data, of the PHP script you are running. You will have to initialize the background script as if you are making a completely new request.
当然,该 PHP 脚本不会共享您正在运行的 PHP 脚本的状态或任何数据。您将必须初始化后台脚本,就像您正在发出一个全新的请求一样。
回答by Layke
Since PHP does not have the ability to support multithreading, I don't quite know how to advise you. Each time a new script is loaded, a new instance of PHP is loaded, so if your server can handle X many PHP instances, then you can do what you want.
由于PHP不具备支持多线程的能力,我不太知道如何建议您。每次加载新脚本时,都会加载一个新的 PHP 实例,因此如果您的服务器可以处理 X 个 PHP 实例,那么您就可以做您想做的事。
Here is something however:
然而,这里有一些东西:
Code for background execution
后台执行代码
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
?>
回答by Karsten
Do you really want to have multi-threadingin php?
你真的想在 php 中使用多线程吗?
OR do you just want to execute a php script every second? For the latter case, a cronjob-like "execute this file every second" approach via linux console tools should be enough.
或者你只想每秒执行一个 php 脚本?对于后一种情况,通过 linux 控制台工具使用类似于 cronjob 的“每秒执行此文件”的方法就足够了。
回答by Sam Dark
If your task is to make a lot of HTTP requests you can use curl multi. There is a good library for doing it: http://code.google.com/p/rolling-curl/
如果您的任务是发出大量 HTTP 请求,您可以使用 curl multi。有一个很好的库可以这样做:http: //code.google.com/p/rolling-curl/
回答by Alex Weber
As everyone's already mentioned, PHP doesn't natively support multi-threading and the workarounds are, well, workarounds...
正如每个人已经提到的,PHP 本身并不支持多线程,解决方法是,嗯,解决方法......
That said, have you heard of the Facebook PHP Compiler? Basically it compiles your PHP to highly optimized C++ and uses g++ to compile it. This opens up a world of opportunities, including but not limited to multi-threading!
也就是说,您听说过Facebook PHP Compiler吗?基本上它将您的 PHP 编译为高度优化的 C++ 并使用 g++ 编译它。这开辟了一个充满机遇的世界,包括但不限于多线程!
The project is open-source and its on github
该项目是开源的,它在github 上
回答by coder
if you just want to post a HTTP request, just do it using PHP CURL lib. It will solve your issue.
如果您只想发布 HTTP 请求,只需使用 PHP CURL 库即可。它将解决您的问题。

