happyslucker/parallel

🚀 High-performance parallel processing & async library for PHP. Process-based parallelism with promise chains, worker pools, and zero dependencies.

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Forks: 0

pkg:composer/happyslucker/parallel

v2.0.0 2025-10-21 22:48 UTC

This package is not auto-updated.

Last update: 2025-10-21 20:25:40 UTC


README

A powerful multiprocessing and async library for PHP that makes parallel programming simple and enjoyable! 🚀

Table of Contents 📚

Features ✨

FeatureDescription
🔥 Parallel MapProcess arrays in parallel
Async TasksRun background tasks
🔗 Promise Chainsthen()->then()->await() pattern
🎯 Process PoolManaged worker processes
🚦 Auto SerializationSmart data serialization
⏱️ Timeout ControlConfigurable timeouts

Requirements 🛠️

Required

  • PHP 8.3+ 🐘
  • pcntl extension 🔧

Recommended

  • msgpack extension 📦 (for better performance)

Installation 📦

composer require happyslucker/parallel

Quick Start 🚀

use Parallel\Parallel;
use Parallel\Async\Async;

// Parallel array processing
$results = Parallel::map(
    fn($item) => $item * 2,
    [1, 2, 3, 4, 5],
    4 // concurrent processes
);

print_r($results); // [2, 4, 6, 8, 10]

// Async task
$future = Async::run(function($name) {
    sleep(2);
    return "Hello, $name!";
}, ["World"]);

echo "Task running in background...\n";
$result = $future->await();
echo $result; // "Hello, World!"

Core Concepts 🧠

Process Pool Architecture 🔄

Main Process
    ├── Process Pool (Manages workers)
    │   ├── Worker 1 ──→ Task 1
    │   ├── Worker 2 ──→ Task 2  
    │   └── Worker 3 ──→ Task 3
    └── Future (Handles results)

Serialization Methods 📊

Method⚡ Speed📦 Size🛠️ Requires
MsgPack🚀 Fast🔻 Smallext-mspack
JSON🐢 Slow🔺 LargeBuilt-in

Examples 📖

1. Parallel Array Processing

use Parallel\Parallel;

$data = range(1, 10);

$results = Parallel::map(
    function($number) {
        usleep(100000); // Simulate work
        return [
            'number' => $number,
            'square' => $number * $number,
            'pid' => getmypid()
        ];
    },
    $data,
    4, // concurrency
    5.0 // timeout
);

foreach ($results as $result) {
    echo "Number: {$result['number']}, Square: {$result['square']}, PID: {$result['pid']}\n";
}

2. Async Tasks with Promise Chains ⛓️

use Parallel\Async\Async;

// Complex async pipeline with multiple then() chains
$future = Async::run(function() {
    echo "🏁 Starting initial task...\n";
    sleep(1);
    return 10;
})
->then(function($result) {
    echo "🔢 Processing result: $result\n";
    sleep(1);
    return $result * 2; // 20
})
->then(function($result) {
    echo "📊 Analyzing data: $result\n";
    sleep(1);
    if ($result > 15) {
        return ["status" => "success", "value" => $result + 5];
    }
    return ["status" => "error", "value" => $result];
})
->then(function($result) {
    echo "🎯 Final transformation...\n";
    sleep(1);
    if ($result['status'] === 'success') {
        return "🎉 Final result: " . $result['value'];
    }
    return "❌ Operation failed: " . $result['value'];
});

echo "⏳ Pipeline executing in background...\n";

// Wait for the entire chain to complete
$finalResult = $future->await();
echo $finalResult . "\n"; // 🎉 Final result: 25

3. Real-world Example: Data Processing Pipeline 🛠️

use Parallel\Async\Async;

// Simulate a real data processing pipeline
$processingPipeline = Async::run(function() {
    echo "📥 Step 1: Fetching user data...\n";
    sleep(2);
    return ['user_id' => 123, 'name' => 'John Doe'];
})
->then(function($userData) {
    echo "🔍 Step 2: Fetching user orders...\n";
    sleep(1);
    return [
        'user' => $userData,
        'orders' => [
            ['id' => 1, 'amount' => 100],
            ['id' => 2, 'amount' => 200]
        ]
    ];
})
->then(function($data) {
    echo "💰 Step 3: Calculating statistics...\n";
    sleep(1);
    $totalAmount = array_sum(array_column($data['orders'], 'amount'));
    return [
        'user_info' => $data['user'],
        'order_count' => count($data['orders']),
        'total_spent' => $totalAmount,
        'average_order' => $totalAmount / count($data['orders'])
    ];
})
->then(function($stats) {
    echo "📊 Step 4: Generating report...\n";
    sleep(1);
    return [
        'report' => "User {$stats['user_info']['name']} made {$stats['order_count']} " .
                   "orders totaling \${$stats['total_spent']}",
        'timestamp' => time(),
        'data' => $stats
    ];
});

// Do other work while pipeline runs
echo "🚀 Pipeline started, doing other work...\n";

// Get final result
$report = $processingPipeline->await();
echo "✅ " . $report['report'] . "\n";
echo "📅 Report generated at: " . date('Y-m-d H:i:s', $report['timestamp']) . "\n";

4. Error Handling in Chains ❌

use Parallel\Async\Async;

$future = Async::run(function() {
    echo "🎲 Rolling dice (WoW players will understand :) )...\n";
    sleep(1);
    $roll = rand(1, 6);
    if ($roll < 3) {
        throw new Exception("Bad roll: $roll! 😞");
    }
    return $roll;
})
->then(
    function($result) {
        echo "🎯 Good roll: $result\n";
        return $result * 10;
    },
    function($error) {
        echo "🛑 Error handled: {$error->getMessage()}\n";
        return 0; // Recover with default value
    }
)
->then(function($result) {
    echo "💰 Points awarded: $result\n";
    return $result;
});

$finalPoints = $future->await();
echo "🏁 Game over. Total points: $finalPoints\n";

Best Practices 🏆

Performance Optimization 🚀

Scenario🎯 Recommended Approach🔄 Concurrency
CPU-intensiveParallel::map()💻 CPU cores
I/O operationsAsync::run()⚡ Higher (8-16)
Mixed workload🔀 Combination🎛️ Adjustable

API Reference 📚

🏗️ Main Classes

Class🎯 Purpose⚙️ Methods
Parallel🔧 Static utilitiesmap(), mapConcurrent(), awaitAll()
Async⚡ Async task runnerrun()
ProcessPool👥 Process managementsubmit(), waitAll(), reap()
Future📦 Result handlingawait(), isRunning(), pid()
AsyncFuture🔮 Promise-like APIawait(), then(), isReady()

License 📄

MIT License - feel free to use in your projects! 💙

Author's comment

Let there be asynchronous PHP!