面向对象的PHP中的非阻塞HTTP请求? [英] Non-blocking HTTP requests in object-oriented PHP?

查看:229
本文介绍了面向对象的PHP中的非阻塞HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与RESTful服务器连接的PHP客户端应用程序。客户端上的每个PHP Goat实例都需要根据服务器上的/ goat请求中的信息来初始化自己(例如/ goat / 35,/ goat / 36等)。它通过cURL向其相应的URL发送HTTP请求来完成此操作。每页加载超过30个山羊对象加载相当于30多个HTTP请求,每个请求需要0.25秒 - 这就像我的山羊会说的那样是baaaad。延迟加载和缓存内存中的响应有帮助,但还不够。

I have a PHP client application that is interfacing with a RESTful server. Each PHP Goat instance on the client needs to initialize itself based on information in a /goat request on the server (e.g. /goat/35, /goat/36, etc.). It does this by sending an HTTP request to its corresponding URL via cURL. Working with 30+ goat objects per page load equates to 30+ HTTP requests, and each one takes 0.25 second - that's baaaad, as my goats would say. Lazy-loading and caching the responses in memory helps, but not enough.

foreach ($goats as $goat) {
   $goat->getName() // goat needs to hit the REST API
}

这种技术的优点是我的山羊都很聪明并且是封装的。缺点是性能太可怕了。山羊不知道如何排队他们的HTTP请求,一只山羊不知道是否有其他山羊需要发起请求等等。我猜一种方法是在外部建立山羊:

The advantage of this technique is that my goats are all smart and encapsulated. The disadvantage is that the performance is horrible. The goats don't know how to queue their HTTP requests, one goat doesn't know if there are other goats that need to initiate a request, etc. I guess one alternative would be to build the goats externally:

$urls = array('http://', 'http://', ...);  // array of goat URLs
$result = fancy_pipelined_http_request_queue($urls);
foreach ($result as $xml) {
   $goat->buildSelfFromXML($xml);
}

我确信这是一个众所周知的OO / REST困境是更先进的解决方法,我只是不知道在哪里看。任何想法?

I'm sure this is a well-known OO/REST dilemma that there are more advanced ways of solving, I just don't know where to look. Any ideas?

推荐答案

如果您愿意,可以使用非阻塞套接字。这需要一些编码来切换到它们,因为你需要将卷曲放在一边。但这可能会提高性能,因为您真的可以同时执行请求。

You can use non-blocking sockets if you like. This involves a bit of coding to switch to them as you will need to kick curl aside. But this may improve performance because you will really be able to perform requests simultaneously.

参见 socket_set_blocking / stream_set_blocking 功能。

这篇关于面向对象的PHP中的非阻塞HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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