pigfox
pigfox

Reputation: 1401

Parallel/Asynchronous PHP

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.

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.

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.

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 waits for the slowest response to return all responses. This is the current approach, no parallel response processing.

  2. pthreads not compatible with Apache, command line only.

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

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

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

Any suggestions?

Upvotes: 0

Views: 1269

Answers (1)

Sammitch
Sammitch

Reputation: 32232

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

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.

Responding to your assessment of approaches:

  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.
  2. It's not that pthreads is incompatible with apache, it's that it's incompatible with mod_php.
    • Use use an FCGI worker model like FPM and you can use pthreads all you want.
  3. Why not? That's what serialization is for.
    • 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.
  4. If you're just going to write off solutions wholesale like this you're going to have a bad time, particularly if you're trying to do something that's inherently at odds with PHP, like parallel/async processing.

Upvotes: 1

Related Questions