PHP 中的线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2585656/
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
Threads in PHP?
提问by PHP Ferrari
I am creating a web application using zend, here I create an interface from where user-A can send email to more than one user(s) & it works excellent but it slow the execution time because of which user-A wait too much for the "acknowledged response" ( which will show after the emails have sent. )
我正在使用 zend 创建一个 Web 应用程序,在这里我创建了一个界面,用户 A 可以从中向多个用户发送电子邮件,并且它运行良好,但它会减慢执行时间,因为用户 A 等待太多“已确认的回复”(将在电子邮件发送后显示。)
In Java there are "Threads" by which we can perform that task (send emails) & it does not slow the rest application.
在 Java 中有“线程”,我们可以通过它来执行该任务(发送电子邮件)并且它不会减慢其余应用程序的速度。
Is there any technique in PHP/Zend just like in Java by which we can divide our tasks which could take much time eg: sending emails.
PHP/Zend 中是否有任何技术可以像在 Java 中一样,我们可以通过它来划分可能需要很多时间的任务,例如:发送电子邮件。
回答by clyfe
EDIT (thanks @Efazati, there seems to be new development in this direction)
编辑(感谢@Efazati,这个方向似乎有新的发展)
http://php.net/manual/en/book.pthreads.php
Caution: (from hereon the bottom):
http://php.net/manual/en/book.pthreads.php
注意:(从这里底部):
pthreads was, and is, an experiment with pretty good results. Any of its limitations or features may change at any time; [...]
pthreads 过去是,现在也是,一个取得相当不错结果的实验。其任何限制或功能可能随时更改;[...]
/EDIT
/编辑
No threads in PHP!
PHP 中没有线程!
The workaroundis to store jobs in a queue (say rows in a table with the emails) and have a cronjob call your php script at a given interval (say 2 minutes) and poll for jobs. When jobs present fetch a few (depending on your php's install timeout) and send emails.
该解决方法是存储作业在队列(说行与电子邮件的表),并有一个cronjob打电话给你的PHP脚本在给定的时间间隔(比如2分钟),轮询工作。当工作出现时获取一些(取决于您的 php 安装超时)并发送电子邮件。
The main idea to defer execution:
推迟执行的主要思想:
- main script adds jobs in the queue
- cron script sends them in tiny slices
- 主脚本在队列中添加作业
- cron 脚本将它们分成小片发送
Gotchas:
陷阱:
- make sure u don't send an email without deleting from queue (worst case would be if a user rescieves some spam at 2 mins interval ...)
- make sure you don't delete a job without executing it first ...
- handle bouncing email using a score algorithm
- 确保你不要在没有从队列中删除的情况下发送电子邮件(最坏的情况是用户每隔 2 分钟收到一些垃圾邮件......)
- 确保在没有先执行作业的情况下不要删除作业......
- 使用评分算法处理退回的电子邮件
回答by Doug Kavendek
You could look into using multiple processes, such as with fork. The communication between them wouldn't be as simple as with threads (but then, it won't come with all of its pitfalls either), but if you're just sending emails, it might not be necessary to communicate much, if at all.
您可以考虑使用多个进程,例如fork。它们之间的通信不会像线程那样简单(但是,它也不会带来所有陷阱),但是如果您只是发送电子邮件,则可能没有必要进行太多通信,如果在全部。
回答by Kevin Schroeder
Watch out for doing forks on an Apache process. You may get some behaviors that you are not expecting. If you are looking to do any kind of asynchronous execution it should be via some kind of queuing mechanism. Gearman is one. Zend Server Job Queue is another. I have some demo code at Do you queue? Introduction to the Zend Server Job Queue. Cron can be used, but you'll have the problem of depending on your cron scheduler to run tasks whereas asynchronous computing often needs to be run immediately. Using a queuing system allows you to do that without threading.
注意在 Apache 进程上进行分叉。您可能会出现一些您意想不到的行为。如果您希望进行任何类型的异步执行,则应该通过某种排队机制。Gearman就是其中之一。Zend Server 作业队列是另一个。我有一些演示代码,你排队吗?Zend 服务器作业队列简介。可以使用 Cron,但是您会遇到依赖于您的 cron 调度程序来运行任务的问题,而异步计算通常需要立即运行。使用排队系统可以让你在没有线程的情况下做到这一点。
回答by JasonDavis
There is a Threading extension being developed based on PThreads that looks promising at https://github.com/krakjoe/pthreads
有一个基于 PThreads 的线程扩展正在开发中,在https://github.com/krakjoe/pthreads看起来很有希望
回答by troelskn
回答by Elzo Valugi
You may want to use a queue system for your email sending and send the email from another system which supports threads. PHP is just a tool and you should the tool that is best fitted for the job.
您可能希望使用队列系统发送电子邮件,并从另一个支持线程的系统发送电子邮件。PHP 只是一个工具,您应该选择最适合这项工作的工具。
回答by manugupt1
As of PHP there are no threads in it. However for php, you can have a look at this roundabout way http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html
从 PHP 开始,它没有线程。但是对于 php,你可以看看这个迂回的方式 http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html
回答by Neil Aitken
PHP doesn't include threading as part of the language, there are some methods that can emulate it but they aren't foolproof.
PHP 不包含线程作为语言的一部分,有一些方法可以模拟它,但它们不是万无一失的。
This Google searchshows a few potential workarounds
此 Google 搜索显示了一些潜在的解决方法

