并行/异步PHP [英] Parallel/Asynchronous PHP

查看:68
本文介绍了并行/异步PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是重建一个应用程序(CakePHP 2.0,php 5.6),该应用程序接收一个请求,将主体重新格式化/映射到特定于API的字段,并使用新映射的主体向多个API发出请求。

I have been tasked with rebuilding an application (CakePHP 2.0, php 5.6) that receives a request, reformats/maps the body to API specific fields and makes requests to multiple APIs with the newly mapped body.

一旦响应返回,它们将被解码并作为应用程序的响应放入输出数组中。

Once the responses are coming back they will be decoded and placed in the output array as a response from the application.

当前解码(从API特定字段进行映射),一旦Multicurl请求返回,流程就会依次发生。

Currently the decoding (mapping from API specific fields) process happens in sequence once the Multicurl requests return.

我的想法是处理响应,并在响应到达后立即进行,而我试图

My idea is to process the responses and soon as they arrive, and I am attempting to do so in parallel.

一个复杂性是每个目标API需要4个非常特定的映射函数,因此每个API对象都有2个不同操作的映射和反向映射。

One complexity is that every target API needs 4 very specific mapping functions so every API object has a map and reverse map for 2 different operations.

客户端的要求是使依赖项的数量最少,解决方案最好使用原始php,而不需要任何库。
已要求KISS解决方案。

A client requirement is to have the minimum number of dependencies, the solution should preferably be in raw php, no libraries wanted. The KISS solution has been requested.

我考虑过以下方法,但它们都有缺点。

I have considered the following approaches but they all have drawbacks.


  1. Multicurl等待最慢的响应以返回所有响应。这是当前的方法,没有并行响应处理。

  1. Multicurl waits for the slowest response to return all responses. This is the current approach, no parallel response processing.

pthreads与Apache不兼容,仅命令行。

pthreads not compatible with Apache, command line only.

不能轻松地通过套接字传递复杂的对象(API对象)。

Can't pass complex objects (API object) via Sockets easily.

依赖性过多和/或不成熟。
a)Appserver
b)Kraken
c)RabbitMQ
d)socket.io

Too many dependencies and/or too immature. a) Appserver b) Kraken c) RabbitMQ d) socket.io

我正在寻找此任务的PHP 7替代品。

I am looking for PHP 7 (nothing else) alternatives to this task.

有任何建议吗?

推荐答案

值得注意的是,并行和异步是分开的概念。

It's worth noting that 'parallel' and 'asynchronous' are separate concepts.

例如:ReactPHP和它的ilk [node。包含js]是异步的,但仍是单线程的,依靠事件循环,回调和协程来允许代码的乱序执行。

eg: ReactPHP and it's ilk [node.js included] are asynchronous, but still single-threaded, relying on event loops, callbacks, and coroutines to allow out-of-order execution of code.

方法评估:


  1. 准确评估 curl_multi()


    • 但是,您陈述的情况是所有这些都需要在单个请求的上下文中进行,所以无论您要做什么在您提供您的响应之前,必须等待最慢的API响应。

    • 除非您从根本上改变了工作流程,否则您可能应该坚持这样做。

  1. Accurate assessment of curl_multi().
    • However, your stated case is that all of this needs to take place within the context of a single request, so no matter what you do you're going to be stuck waiting on the slowest API response before you can serve your response.
    • Unless you're fundamentally changing your workflow you should probably just stick with this.

  • 使用使用FCGI工人模型,例如 FPM ,您可以随意使用pthreads。

  • Use use an FCGI worker model like FPM and you can use pthreads all you want.
  • So long as you never accept it from users or want to use it outside of PHP, serialize() is one option.
  • In pretty much all other cases json_encode() is going to be the way to go.

这篇关于并行/异步PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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