gpt4 book ai didi

php - 如何在 python 或 ruby​​ 中执行此操作(PHP)?

转载 作者:行者123 更新时间:2023-12-01 06:16:04 25 4
gpt4 key购买 nike

我的应用程序需要一个很长的 url 列表,并将其拆分为 X(其中 X = $threads),这样我就可以启动一个 thread.php 并计算它的 url。然后它执行 GET 和 POST 请求来检索数据

我正在使用这个:

for($x=1;$x<=$threads;$x++){
$pid[] = exec("/path/bin/php thread.php <options> > /dev/null & echo \$!");
}

对于“线程”(我知道它不是真正的线程,是 fork 还是什么?),我将 pid 保存到一个文件中,以便稍后检查 N 个线程是否正在运行并停止它们。

现在我想离开 php,我正在考虑使用 python,因为我想了解更多有关它的信息。

如何使用 python 实现这种“线程”?(或 ruby​​)

或者是否有更好的方法在 python 或 ruby​​ 中启动并行(同时)运行的多个后台线程?

线程不需要相互通信或与主线程通信,它们是独立的,它们执行http请求并与mysql数据库交互,它们可能需要访问/修改相同的表条目(我没有)还没有考虑过这个问题或者我将如何解决它)。

该应用程序与“项目”一起使用,每个项目都有一个“最大线程”变量,我使用 Web 界面来控制它(因此我仍然可以在新应用程序中使用 php 作为界面 [启动/停止线程]) .

我想使用

from threading import Thread

在 python 中,但我被告知这些线程不会并行运行,而是一次运行一次。

该应用程序旨在在 Linux Web 服务器上运行。

如有任何建议,我们将不胜感激。

最佳答案

对于 Python 2.6+,请考虑 multiprocessing模块:

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows

对于 Python 2.5,可以通过 pyprocessing 获得相同的功能。 .

除了上面链接中的示例之外,这里还有一些可帮助您入门的附加链接:

关于php - 如何在 python 或 ruby​​ 中执行此操作(PHP)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3294917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com