Asynchronous Programming in Scriptcase

I have been successful using CURL within Scriptcase to run applications Asynchronous… but I would really like a way to run functions Asynchronous from within a library.

Ive been searching all over to find a way to run a function from an internal library Asynchronous in order to speed up its operation… but have not been able to come up with anything to speak of.

Is it possible for me to call a function at the exact same time within PHP and Scriptcase?

Thanks!

Just an update here for my future self. So far the answer is “no” you can’t run two functions, or the same function asynchronously… At best your functions could return a “stub” or “token” and kick of another operation that takes time… and your calling script could use that stub or token to check if the work was completed or not later in the script.

Using CURL works to separate things into multiple processes and you can use Curl_Multi to run several at the same time… if what you are calling is web services, then it works for aggregating several web service calls into one asynchronous call with all responses coming back at the speed of the slowest response.

I use RabbitMQ. In my case, I want to process video diagnostics scripts which take some time to finish. Instead of checking if one diagnostic have finished and manually executing each diagnostic job, I use a message broker to handle the management. Basically, I produce messages in JSON that contain all the information of the jobs (one msg per Job). Then the message broker distributes the messages to a set of consumers that run an instance of the diagnostic script. The diagnosis script is/are always idle waiting for a message, once one of the consumer finishes its process it acknowledges back to the message broker and is ready to consume another message in queue. This way I can leave the jobs running asynchronously during the weekend without my supervision. My limitation is the hardware, I can run as many consumers as my hardware performance allows me.

Hope it helps,
XNT

Hi Junior,

Can you explain how to use RabbitMQ with SC?

Assuming that you have a basic understanding of RabbitMQ and is already installed. You would ideally use AMQP library to communicate with the broker, but I encountered difficulties making it work with SC, although I didn’t invest much effort in attempting to do so. Given that my specific requirement was solely to send messages from SC, I opted to utilize the RabbitMQ built-in API for message publication. For the consumers, I rely on plain PHP with AMQP, without incorporating SC or other frameworks.

API Example
  <?php

$queueName = ‘your_queue_name’;
$baseUrl = ‘http://localhost:15672/api’; // RabbitMQ management API URL
$username = ‘your_username’;
$password = ‘your_password’;

// Message to send
$message = ‘Hello World’;

// Create a cURL handle
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $baseUrl . ‘/queues/%2F/’ . $queueName . ‘/publish’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([‘properties’ => [], ‘routing_key’ => $queueName, ‘payload’ => $message, ‘payload_encoding’ => ‘string’]));
curl_setopt($ch, CURLOPT_USERPWD, $username . ‘:’ . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, [‘Content-Type: application/json’]);

// Execute cURL request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
echo 'Error: ’ . curl_error($ch);
} else {
echo 'Message sent to the queue: ’ . $message;
}

// Close cURL handle
curl_close($ch);

2 Likes

Thank you for the API Example code and the follow up… I too will follow up with my experience. I have just completed a system for my client that handles jobs asynchronously… all using MySQL 8.0 with its ability to lock rows and hide them on request using this as a guide: https://medium.com/datadenys/implementing-simple-job-queue-with-mysql-8-0-and-php-pdo-6023724ace99

Hopefully that helps someone. It’s working perfectly so far!

2 Likes