I am developing a WordPress plugin and attempting to use the AmpPHP library to handle asynchronous tasks. I have set up my plugin with the following code:
require_once plugin_dir_path(__FILE__) . 'SMSService.php';
require_once plugin_dir_path(__FILE__) . 'EmailService.php';
require_once plugin_dir_path(__FILE__) . '/../libraries/Amp/vendor/autoload.php';
use function Ampasync;
use function Ampdelay;
class MessageService
{
public $smsService;
public $emailService;
public function __construct()
{
}
public function sendSMS($to, $body, $from = null)
{
$this->smsService = new SMSService();
async(function () use ($to, $body, $from) {
$this->smsService->handle($to, $body, $from);
});
}
}
In my implementation, I call the sendSMS method, but the asynchronous code inside async does not seem to execute.
I installed the AmpPHP library using composer require amphp/amp. After calling the sendSMS method, I also attempted to run EventLoop::run() to process the asynchronous tasks. However, despite this, the sendSMS method executes synchronously.
I expected the asynchronous function to run separately and not block the execution of subsequent code. Could someone help me understand why the async function is not working as expected in this setup?