在 PHP 中运行异步函数的最有效方法是什么?

发布于 2024-12-10 05:22:46 字数 456 浏览 1 评论 0原文

我正在开发一个应用程序,该应用程序利用主要用 PHP 编写的 Amazon MWS API。该应用程序允许亚马逊卖家注册,提供一些亚马逊卖家凭据,然后该应用程序开始从亚马逊下载该用户的订单,并将其放入 MySQL 数据库中。

我已经使用具有多个函数的脚本构建了将数据准确同步到数据库的方法,但注意到这需要太长时间。该脚本只是循环遍历数据库中的所有用户,并一次迭代一个订单。现在,只有 5 个测试用户,时间是可行的,但我正在寻找一种更具可扩展性的方法。想象一下 500 个用户一次都同步运行。太长了!

我对 PHP 还很陌生,尤其是从中运行异步进程。我发现可以做到这一点的唯一方法是使用一个启动脚本来查找数据库中的所有用户,并为每个用户生成一个同步脚本,然后释放它。我不喜欢这个想法,因为如果我确实有 500 多个用户,我的夜间同步将包含该 PHP 脚本的 500 个生成实例。

以前有人做过类似的事情吗?如果是这样,我很想听听如何最好地提高同步效率。

I'm working on an application that utilizes Amazon's MWS API written mostly in PHP. This application allows Amazon sellers to sign up, provide some Amazon seller credentials and the application then begins to download this user's orders from Amazon placing them into a MySQL database.

I've built the methods to sync data accurately to the database using a script with multiple functions but noticed this takes way too long. This script simply loops through all users in the database and iterates all orders one at a time. Right now, with only 5 test users, the time is doable but I'm looking for a more extensible method. Think about 500 users all running synchronously one at a time. WAY too long!

I'm pretty new to PHP and especially running asynchronous processes from it. The only way I've found that I can do this is to have a starter script that finds all users in the database and spawns a sync script for each user then releases it. I don't like this idea because if I did had 500+ users, my nightly sync would consist of 500 spawned instances of this PHP script.

Has anyone done something similar to this before? If so, I'd love to hear how best to make this sync more efficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东风软 2024-12-17 05:22:46

由于 PHP 不能是多线程1,因此实际上您只有 2 个选择(有多种方法可以做到这一点,但它们都归结为以下类别):

  • 每个用户操作有 1 个进程。正如您所说,对于大型数据库,这可能会导致很多进程。
  • 拥有一个处理多个用户的流程。这可能需要更长的时间,因为每个进程实际上都是同步的。

我认为最好的选择是将这两种方法结合起来,这样你就有多个处理多个用户的进程。因此,如果您有 500 个用户,则生成 100 个进程,每个进程处理 5 个用户,或者生成 50 个进程,每个进程处理 10 个用户。

或者,可能值得使用更适合该任务的语言(支持多线程的语言,如 Java 或 Perl)编写程序,如果需要,您可以从 PHP 开始。

1编辑 03/2013:PHP 现在可以是多线程的,但不建议将其用于生产用途,如 pthreads 扩展仍然非常不稳定,也不推荐普通用户使用,只有当你真正知道自己在做什么时才使用它

Since PHP cannot be multi-threaded1, in practice you have only 2 choices (there are several methods to do this, but they all boil down to the following to categories):

  • Have 1 process per user operation. As you say, with a large database, this could result in a lot of processes.
  • Have a process that deals with more than one user. This will potentialy take longer, since each process is effectively synchronous.

I think the best bet is to combine the two approaches, so you have multiple processes that deal with multiple users. So if you had 500 users, spawn 100 processes that deal with 5 users each, or 50 processes that deal with 10 users each.

Alternatively, it might be worth writing a program in a language that is better suited to the task - something that supports multi-threading, like Java or Perl - that you can start from PHP if required.

1Edit 03/2013: PHP can now be multithreaded, but its not recommended for production use as the pthreads extension is still highly unstable, and it is also not recommended for the average user, only use this if you really know what you are doing

提赋 2024-12-17 05:22:46

同意 DaveRandom,请查看 HTTP 请求池的实现,而不是滚动自己的 http ://www.php.net/manual/en/class.httprequestpool.php

Agree w/DaveRandom, check out HTTP Request Pool for implementation, rather than rolling your own http://www.php.net/manual/en/class.httprequestpool.php

许仙没带伞 2024-12-17 05:22:46

我最终使用了 passthru 并将脚本传递到后台。到目前为止,一切似乎进展顺利。

I ended up using passthru and passing the script to the background. Things seem to be working well so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文