Last active
October 3, 2023 15:53
-
-
Save nei/4ffd278c627890d3b7cf1ff66b170457 to your computer and use it in GitHub Desktop.
Example of reactphp usage to read lines of a file and raise X concurrent requests to an endpoint to check each line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"react/stream": "1.x-dev", | |
"react/event-loop": "1.x-dev", | |
"react/http": "1.x-dev", | |
"clue/mq-react": "^1.6" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sku1 | |
sku2 | |
sku3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Clue\React\Mq\Queue; | |
use Psr\Http\Message\ResponseInterface; | |
use React\Stream\ReadableResourceStream; | |
$loop = React\EventLoop\Loop::get(); | |
$browser = new React\Http\Browser($loop); | |
$filename = 'list-of-skus.txt'; | |
$endpoint = 'https://example.com?productCode='; | |
$concurrency = 16; | |
// each job should use the browser to GET a certain URL | |
// limit number of concurrent jobs here | |
$queue = new Queue($concurrency, null, function($url) use ($browser) { | |
return $browser->get($url, [ | |
'Content-Type' => 'application/json' | |
]); | |
}); | |
$stream = new ReadableResourceStream(fopen($filename, 'r'), $loop); | |
$stream->on('data', function ($data) use ($browser, $queue, &$count, $endpoint) { | |
// Process each line (data) received from the stream | |
$lines = explode("\n", $data); | |
foreach ($lines as $line) { | |
// Do something with each line | |
echo $line . PHP_EOL; | |
$queue($endpoint.$line) | |
->then( | |
function (ResponseInterface $response) use ($line) { | |
$result = json_decode((string) $response->getBody(), true); | |
echo 'product '.($result['Content']['ProductCode']).' exist'.PHP_EOL; | |
}, | |
function (Exception $e) use ($line) { | |
echo 'missing '.$line.PHP_EOL; | |
} | |
); | |
} | |
}); | |
// Handle errors | |
$stream->on('error', function (Exception $e) { | |
echo 'Error: ' . $e->getMessage() . PHP_EOL; | |
}); | |
// Handle end of file (EOF) | |
$stream->on('end', function () use ($loop) { | |
echo 'finished raising requests'; | |
// Close the event loop when the stream ends (EOF) | |
}); | |
// Start the event loop | |
$loop->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment