PHP Classes

File: examples/demo.php

Recommend this page to a friend!
  Packages of Nahid Bin Azhar   Task PHP   examples/demo.php   Download  
File: examples/demo.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Task PHP
Run in parallel PHP code tasks and await for them
Author: By
Last change:
Date: 3 months ago
Size: 888 bytes
 

Contents

Class file image Download
<?php

require __DIR__ . '/../vendor/autoload.php';

use
Nahid\TaskPHP\Task;

// 1. Basic Async/Await
echo "--- Running tasks concurrently and awaiting results ---\n";
$vals = Task::async([
   
'val' => function () {
       
sleep(2);
        return
"5";
    }
])->
await();

print_r($vals);

// 2. Await with callback processing
echo "\n--- Processing results with a callback ---\n";
$sum = Task::async([
   
'a' => fn() => 10,
   
'b' => fn() => 20
])->await(fn($res) => array_sum($res));

echo
"Sum: $sum\n";

// 3. Fire and Forget (Background)
echo "\n--- Dispatched background task. Check background.log in 2 seconds ---\n";
Task::async([
   
'bg' => function () {
       
sleep(2);
       
file_put_contents(__DIR__ . '/background.log', "Background job finished at " . date('H:i:s') . "\n");
    }
])->
forget();

echo
"Main script exiting. Worker is still running in background!\n";