G
G
Gleb2017-10-26 12:58:44
PHP
Gleb, 2017-10-26 12:58:44

How to organize multi-threaded access to third-party APIs?

Colleagues, good afternoon!
I am writing a service that accesses a dozen different APIs, requires data from them, receives them and processes them.
The problem is the response time from each API.
What is the best way to implement such multithreading, given that you need to wait until all services respond, then process all the information in aggregate and give the result to the user?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton B, 2017-10-26
@bigton

Let's say you are creating a new unique service for finding cheap flights ;)
1. The user enters the page of your service, selects the direction (city) and departure date (date), clicks Find.
2. An AJAX POST request is sent to /search.php {city, date}, which returns the request ID (request_id).
3. Every 1-5 seconds, an AJAX GET request is sent to /search.php?check=request_id, which returns the percentage of response completed (0-100%).
4. When readiness reaches 100%, an AJAX GET request is sent to /search.php?result=request_id which returns information about the tickets found.
1. Creating a job queue for a request to airlines
- creating a request ID
$request_id = INSERT INTO `request` (`city`, `date`) VALUES (city, date);
- creating a queue of tasks for obtaining information on tickets
INSERT INTO `request_task` (`request_id`, `airline_id`, `status`) VALUES (request_id, 1, 0);
INSERT INTO `request_task` (`request_id`, `airline_id`, `status`) VALUES (request_id, 2, 0);
INSERT INTO `request_task` (`request_id`, `airline_id`, `status`) VALUES (request_id, 3, 0);
INSERT INTO `request_task` (`request_id`, `airline_id`, `status`) VALUES (request_id, 4, 0);
2. Checking if the response is ready
SELECT COUNT(*) FROM `request_task` WHERE `request_id` = request_id AND `status` > 0;
3. Information about tickets found
SELECT * FROM `request_result` WHERE `request_id` = request_id
1. Reserve a task for execution
$process = ip2long($_SERVER['SERVER_ADDR']).'.'.getmypid().'.'.microtime(TRUE);
UPDATE `request_task` SET `process` = $process WHERE `status` = 0 LIMIT 1;
2. Get the booked task
SELECT * FROM `request_task` WHERE `process` = $process
3. Call the desired airline
if (airline_id == 1) request to Aeroflot
4. Put the result of the call in `request_result`
5. UPDATE `request_task` SET `process` = 0, `status` = 1 WHERE `process` = $process
Thus multithreading is implemented!

C
coderisimo, 2017-10-26
@coderisimo

Is this suitable?
php.net/manual/en/function.curl-multi-exec.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question