parallite/parallite-php

Standalone PHP client for Parallite - Execute PHP closures in true parallel

Installs: 12

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 1

Forks: 1

Open Issues: 0

pkg:composer/parallite/parallite-php

v1.1.0 2025-10-27 03:08 UTC

This package is auto-updated.

Last update: 2025-11-27 03:20:16 UTC


README

Parallite Logo

Parallite {PHP Client}

Latest Version PHP Version License

Execute PHP closures in true parallel - A standalone PHP client for Parallite, enabling real parallel execution of PHP code without the limitations of traditional PHP concurrency.

โœจ Features

  • ๐Ÿš€ True Parallel Execution - Execute multiple PHP closures simultaneously
  • ๐ŸŽฏ Simple async/await API - Familiar Promise-like interface
  • ๐Ÿ”„ Promise Chaining - Chainable then(), catch(), and finally() methods
  • ๐ŸŒ Cross-platform - Works on Windows, Linux and macOS
  • โฏ๏ธ๏ธ Automatic Daemon Management - Optional auto-start/stop of Parallite daemon
  • โšก Binary MessagePack Transport - Ultra-fast daemon communication (2-5x faster than JSON)

๐Ÿ“‹ Requirements

  • PHP 8.3+
  • ext-sockets
  • ext-zip
  • rybakit/msgpack
  • opis/closure

โš ๏ธ Important Notice:

Passing closures that capture $this will cause opis/closure to serialize the entire object instance. This often includes nonโ€‘serializable dependencies (e.g., PDO, CurlHandle, resource, sockets, Laravel Models, Collections, etc) and may lead to errors.

๐Ÿ‘‰ Please review the Troubleshooting page for guidance on how to avoid this issue and know more.

๐Ÿ“ฆ Installation

composer require parallite/parallite-php

Add the install/update scripts to your composer.json:

{
  "scripts": {
    "post-install-cmd": [
      "@php vendor/parallite/parallite-php/bin/parallite-install"
    ],
    "post-update-cmd": [
      "@php vendor/parallite/parallite-php/bin/parallite-update"
    ]
  }
}

See more about these scripts: Installation Guide.

After adding the scripts, run (to download Parallite binary):

composer install

# or update
composer update

๐Ÿš€ Quick Start

<?php

require 'vendor/autoload.php';

// Basic usage - no imports needed!
$result = await(async(fn() => 'Hello World'));
echo $result; // Hello World

// Parallel execution
$p1 = async(fn() => sleep(1) && 'Task 1');
$p2 = async(fn() => sleep(1) && 'Task 2');
$p3 = async(fn() => sleep(1) && 'Task 3');

$results = await([$p1, $p2, $p3]);
// Total time: ~1s (parallel) instead of 3s (sequential)

// Promise chaining
$result = await(
    async(fn() => 1 + 2)
        ->then(fn($n) => $n * 2)
        ->then(fn($n) => $n + 5)
);
echo $result; // 11

// Error handling
$result = await(
    async(function () {
        throw new Exception('Oops!');
    })->catch(fn($e) => 'Caught: ' . $e->getMessage())
);
echo $result; // Caught: Task failed: Oops!

That's it! The daemon is automatically managed - no manual setup required!

๐Ÿ“š Documentation

โšก Performance

Parallite provides significant speedup for I/O-bound and CPU-bound tasks:

Tasks Sequential Parallel Speedup
3 ร— 1s 3.0s ~1.0s 3.0x
5 ร— 2s 10.0s ~2.0s 5.0x
10 ร— 1s 10.0s ~1.0s 10.0x
Parallelism is beneficial when:

โœ… CPU-intensive operations (image processing, complex calculations)
โœ… Independent I/O-bound operations (external API calls, multiple databases)
โœ… Database with good concurrency (MongoDB, PostgreSQL, SQL Server, MySQL)
โŒ SQLite with concurrent writes
โŒ Operations are already very fast

Example using the Filamentphp demo

Project with + 20 thousand orders, simulating several heavy calculations and with json transformation, as if it were for a heavy dashbaord (when I have time, I'll add this demo project to git):

โœ… With Parallite:

with-parallite

๐Ÿšซ Without Parallite:

with-parallite

The increase was approximately 321.4% in orders processed per second.

Real-World Example

// Fetch multiple APIs in parallel
$promises = [
    'users'    => async(fn() => file_get_contents('https://api.example.com/users')),
    'posts'    => async(fn() => file_get_contents('https://api.example.com/posts')),
    'comments' => async(fn() => file_get_contents('https://api.example.com/comments')),
];

$data = await($promises);
// 3x faster than sequential fetching!

Run the real-world test suite (uses data available at https://jsonplaceholder.typicode.com):

RUN_REAL_WORLD_TESTS=1 vendor/bin/pest tests/Feature/RealWorldDataProcessingTest.php --no-coverage

๐ŸŒ Platform Support

Platform Status Notes
Linux โœ… Fully Supported x86_64, ARM64
macOS โœ… Fully Supported Intel, Apple Silicon
Windows โœ… Fully Supported x86_64, ARM64

๐Ÿ› Troubleshooting

Having issues? Check the Troubleshooting Guide for solutions.

Quick tip:

  • You can use pd() inside async() calls, it will throw an exception with the dump data.
  • Never capture $this in closures passed to async(). Use static methods or extract primitives instead.

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Credits

๐Ÿ“ฎ Support

Made with โค๏ธ by the Parallite community