windows 在 Win 上的 PHP 中 fork/thread 的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/601670/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:05:15  来源:igfitidea点击:

What's the best way to fork/thread in PHP on Win?

phpwindowsmultithreadingfork

提问by Omer

I have a php script that checks for updates on many (thousands) of sites. On occasion (more frequently as the number of sites increases), I get an execution timeout on the update of one of these sites, and the whole script goes down the drain.

我有一个 php 脚本,用于检查许多(数千个)站点上的更新。有时(随着站点数量的增加而更频繁),我会在更新这些站点之一时出现执行超时,并且整个脚本都会付诸东流。

The best idea I could come up with is to fork each update, so if it dies, the overall update just continues. From what I gathered, it seems PHP threading isn't something to rely on, especially on a windows platform (which I unfortunately have to work on).

我能想到的最好的想法是 fork 每次更新,所以如果它死了,整个更新就会继续。从我收集到的信息来看,PHP 线程似乎并不值得依赖,尤其是在 windows 平台上(不幸的是我必须在此平台上工作)。

I thought about using curl as a forking mechanism, but won't it be much more costly for the server (in time and processing power)?

我想过使用 curl 作为分叉机制,但它对服务器(在时间和处理能力方面)的成本不是更高吗?

I'd be happy to hear some ideas and experience about PHP forking/threading...

我很高兴听到一些关于 PHP 分叉/线程的想法和经验......

thanks, Omer.

谢谢,奥马尔。

采纳答案by Omer

Well, at the end I went for curl, and it works just fine.

好吧,最后我选择了 curl,它工作得很好。

I needed a cross-platform solution, as I develop on a Mac while in this case the production serer is Windows. That meant pcntlwas out of the question.

我需要一个跨平台的解决方案,因为我在 Mac 上开发,而在这种情况下,生产服务器是 Windows。这意味着pcntl不可能。

I was worried that sending thousands of curl requests to my own server might hog it and bother users on the site at the time, but I was mistaken.

我当时担心向我自己的服务器发送数千个 curl 请求可能会占用它并打扰网站上的用户,但我错了。

I did, however, have to add a set_time_limit(0)to the script that initiates all the curl calls, otherwise it just times out.

但是,我确实必须在set_time_limit(0)启动所有 curl 调用的脚本中添加一个,否则它就会超时。

回答by Cody Caughlan

If you're going to use cURL look into the multi* family of methods which allows you to streamline cURL and interacting with a set of sites. That being said, you could also set your max execution time (via ini_set) to some arbitrarily high number and/or keep your own watchdog counter so that you never let it get too high.

如果您打算使用 cURL,请查看 multi* 系列方法,它允许您简化 cURL 并与一组站点进行交互。话虽如此,您还可以将最大执行时间(通过 ini_set)设置为某个任意高的数字和/或保留自己的看门狗计数器,以免让它变得太高。

But yeah, ultimately are going to run into issues and the lack of full thread support in PHP. So your best bet is to look at other alternatives, e.g. other languages/platforms that provide native threading support.

但是,是的,最终会遇到问题并且 PHP 中缺乏完整的线程支持。因此,您最好的选择是查看其他替代方案,例如提供本机线程支持的其他语言/平台。

回答by Ronald Conco

I managed to get some form of threading in php using pcntl extension.It was not the best of solutions but it did the trick.

我设法使用 pcntl 扩展在 php 中获得了某种形式的线程。这不是最好的解决方案,但它做到了。

http://www.php.net/manual/en/ref.pcntl.php

http://www.php.net/manual/en/ref.pcntl.php

try the following links also, the gave me a idea how to go about the implementation.

也尝试以下链接,这让我知道如何进行实施。

http://www.van-steenbeek.net/?q=php_pcntl_fork

http://www.van-steenbeek.net/?q=php_pcntl_fork

http://www.hudzilla.org/phpbook/read.php/16_1_3

http://www.hudzilla.org/phpbook/read.php/16_1_3

http://www.electrictoolbox.com/article/php/process-forking/

http://www.electrictoolbox.com/article/php/process-forking/

I hope this helps , but php is not very good with threading though.

我希望这会有所帮助,但是 php 对线程处理不是很好。

回答by Karsten

回答by Ross

You could set the ini directive max_execution_timeto 0. This should remove the max execution time, and allow the script to run without incurring this error. This value has to be set in your php.ini file however - using ini_setdoesn't work.

您可以将 ini 指令设置max_execution_time为 0。这应该会删除最大执行时间,并允许脚本运行而不会产生此错误。但是,必须在 php.ini 文件中设置此值 - 使用ini_set不起作用。