php线程和脚本执行 [英] php threads and script execution

查看:98
本文介绍了php线程和脚本执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php中完成以下行为:

I want to accomplish the following behavior in php:

1-使用参数调用脚本

1 - Script gets called with parameters

2-我启动了一个线程以进行长时间运行

2- I Intiate a thread for a long running operation

3-脚本应将控制权返回给调用者

3 - Script should return control to the caller

4-线程执行直到完成

4- Thread executes till its finished

这种行为可能吗?我现在看到的是,脚本将不会返回,直到线程完成执行为止,这是有道理的,因为如果脚本停止执行,线程的执行可能会死掉,但是没有办法停止阻塞客户端,因此他们可以继续他们的生意吗?我是否坚持使用某些exec()调用来获得此行为?有没有办法仅通过线程来完成此任务?我想尽可能避免使用exec.

Is this behavior possible? What i am seeing now, is that the script wont return until the thread has finished executing, which makes sense as the execution of the thread would probably die if the script stops executing , but is there no way to stop blocking the client so they can go on about their business? Am i stuck using some exec() call to get this behavior? Is there a way to get this done with threading only? Id like to avoid using exec if possible..

因此,如果有人从浏览器调用我的脚本,它应该立即返回,并且长时间运行的过程应该一直执行到完成为止.

So if someone calls my script from a browser, it should just return immidiatly, and the long running process should keep executing until its done.

谢谢

丹尼尔

推荐答案

是的,可能的.通过AJAX调用您的php脚本,并动态创建ajax函数的多个实例.请参阅所附的屏幕截图.当我比较运行单个函数与24个实例的结果时,我的数据处理速度提高了大约15倍.我正在尝试用约3000万条记录填充MySQL表,并且每条记录都涉及基于lat/lng的距离市中心的距离(以英里为单位).是的,它不在公园散步.如您所见,我的平均期望是:

Yes, its possible. Call your php script via AJAX, and and create multiple instances of the ajax function dynamically. See attached screenshot. When I compared results of running a single function versus 24 instances, my data was processed about 15x faster. I am trying to populate a MySQL table with about 30 million records, and each record involves calculating distance in miles from city center, based on lat/lng. So yes, its no walk in the park. As you can see, I am averaging about See this:

多线程http://gaysugardaddyfinder.com/screen2.PNG 多线程http://gaysugardaddyfinder.com/screen.png

这可能是一个光荣的骇客,但事实并非如此-但这确实对我很有用.

This may be a glorious hack or what not - but it sure worked great for me.

我的服务器是具有64 GB RAM的Xeon 72 Core设置.

My server is a Xeon 72 Core setup with 64 GB RAM.

这篇关于php线程和脚本执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆