PHP Classes

File: README.md

Recommend this page to a friend!
  Packages of Nahid Bin Azhar   Task PHP   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Task PHP
Run in parallel PHP code tasks and await for them
Author: By
Last change:
Date: 4 months ago
Size: 1,520 bytes
 

Contents

Class file image Download

TaskPHP

A framework-agnostic PHP concurrency library designed for high-performance task execution using child processes. It provides a modern, clean async/await pattern to manage parallel jobs without complex message brokers.

Installation

composer require nahid/task-php

Features

  • Async/Await Pattern: Start tasks and await results when needed.
  • Concurrency Control: Limit the number of parallel workers.
  • Timeouts: Automatic process termination for slow tasks.
  • Fail-Fast: Optional ability to cancel all tasks if one fails.
  • Framework Agnostic: Works standalone or with Laravel, WordPress, etc.

Basic Usage

1. Simple Async Execution

use Nahid\TaskPHP\Task;

$results = Task::async([
    'task1' => fn() => 10,
    'task2' => fn() => 20,
])->await();

// $results = ['task1' => 10, 'task2' => 20]

2. Await with Result Processing

$sum = Task::async([
    'a' => fn() => 10,
    'b' => fn() => 20
])->await(fn($res) => array_sum($res));

echo $sum; // 30

3. Fire and Forget (Background)

Task::async([
    'send_email' => function() {
        // This runs in the background
    }
])->forget();

Configuration

$results = Task::limit(5)          // Concurrent workers
    ->timeout(30)                  // Max execution time
    ->bootstrap('init.php')        // Bootstrap framework
    ->failFast(true)               // Stop all on first error
    ->async($tasks)
    ->await();

License

MIT (c) Nahid