# zepgram/module-multi-threading

> This module is a powerful tool for developers who want to process large data sets in a short amount of time

`composer require zepgram/module-multi-threading`

Canonical URL: https://packagento.com/zepgram/module-multi-threading

## At a glance

- **Vendor**: zepgram (https://packagento.com/zepgram.md)
- **Latest version**: 0.3.0 — released 2026-02-08
- **Pricing**: Free
- **Package type**: Magento 2 module
- **Status**: active, accepting new buyers

## Installation

Packagento is licence-gated, so even free packages need a licence on a project before Composer can resolve them.

1. **Sign in or create an account** at https://packagento.com/customer/account/.

2. **Add the package to your account.** Open https://packagento.com/zepgram/module-multi-threading and complete the free checkout. A licence is minted automatically.

3. **Create or pick a project, then activate the licence on it.**
   - Projects represent the Magento installs you deploy to. Manage them at https://packagento.com/projects/.
   - Activate the new licence on the project you'll deploy this package to. Activation is what generates the Composer credentials scoped to that project.

4. **Add the project credentials to your Magento codebase.**

   Grab the project's public + private key from https://packagento.com/projects/ (open the project, then its Credentials tab), and add them to `auth.json`:

   ```json
   {
     "http-basic": {
       "packagento.com": {
         "username": "ppk_live_...",
         "password": "psk_live_..."
       }
     }
   }
   ```

   Add the Packagento Composer repository to `composer.json`:

   ```json
   {
     "repositories": [
       { "type": "composer", "url": "https://packagento.com" }
     ]
   }
   ```

5. **Install and apply.**

   ```bash
   composer require zepgram/module-multi-threading:*
   bin/magento setup:upgrade
   bin/magento setup:di:compile
   bin/magento cache:flush
   ```

## What it does

This module is a powerful tool for developers who want to process large data sets in a short amount of time

## README

This module is a powerful tool for developers who want to process large data sets in
a short amount of time. It allows you to process large collections of data in parallel
using multiple child processes, improving performance and reducing processing time.

### Installation
```php
composer require zepgram/module-multi-threading
bin/magento module:enable Zepgram_MultiThreading
bin/magento setup:upgrade
```

### Usage

These classes allows you to process a search criteria, a collection or an array using multi-threading.

#### ForkedSearchResultProcessor

```php
use Zepgram\MultiThreading\Model\ForkedSearchResultProcessor;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;

class MyAwesomeClass
{
    /** @var ForkedSearchResultProcessor */
    private $forkedSearchResultProcessor;
    
    /** @var ProductRepositoryInterface */
    private $productRepository;
    
    public function __construct(
        ForkedSearchResultProcessor $forkedSearchResultProcessor,
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder 
    ) {
        $this->forkedSearchResultProcessor = $forkedSearchResultProcessor;
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }
    
    $searchCriteria = $this->searchCriteriaBuilder->create();
    $productRepository = $this->productRepository;
    $callback = function ($item) {
        $item->getData();
        // do your business logic here
    };
    
    $this->forkedSearchResultProcessor->process(
        $searchCriteria,
        $productRepository,
        $callback,
        $pageSize = 1000,
        $maxChildrenProcess = 10,
        $isIdempotent = true
    );
}
```

#### ForkedCollectionProcessor
```php
use Zepgram\MultiThreading\Model\ForkedCollectionProcessor;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class MyAwesomeClass
{
    /** @var ForkedCollectionProcessor */
    private $forkedCollectionProcessor;

    public function __construct(
        ForkedCollectionProcessor $forkedCollectionProcessor,
        CollectionFactory $collectionFactory
    ) {
        $this->forkedCollectionProcessor = $forkedCollectionProcessor;
        $this->collectionFactory = $collectionFactory;
    }

    $collection = $this->collectionFactory->create();
    $callback = function ($item) {
        $item->getData();
        // do your business logic here
    };

    $this->forkedCollectionProcessor->process(
        $collection,
        $callback,
        $pageSize = 1000,
        $maxChildrenProcess = 10,
        $isIdempotent = true
    );
}
```

#### ForkedArrayProcessor
This class allows you to process an array of data using multi-threading.

```php
use Zepgram\MultiThreading\Model\ForkedArrayProcessor;

class MyAwesomeClass
{
    /** @var ForkedArrayProcessor */
    private $forkedArrayProcessor;
    
    public function __construct(ForkedArrayProcessor $forkedArrayProcessor)
    {
        $this->forkedArrayProcessor = $forkedArrayProcessor;
    }
    
    $array = [1,2,3,4,5,...];
    $callback = function ($item) {
        echo $item;
        // do your business logic here
    };
    
    $this->forkedArrayProcessor->process(
        $array,
        $callback,
        $pageSize = 2,
        $maxChildrenProcess = 2
    );
}
```

#### ParallelStoreProcessor or ParallelWebsiteProcessor

```php
use Zepgram\MultiThreading\Model\Dimension\ParallelStoreProcessor;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class MyAwesomeClass
{
    /** @var ParallelStoreProcessor */
    private $parallelStoreProcessor;
    
    /** @var CollectionFactory */
    private $collectionFactory;
    
    public function __construct(
        ParallelStoreProcessor $parallelStoreProcessor,
        CollectionFactory $collectionFactory
    ) {
        $this->parallelStoreProcessor = $parallelStoreProcessor;
        $this->collectionFactory = $collectionFactory;
    }
    
    $array = [1,2,3,4,5,...];
    $callback = function (StoreInterface $store) {
        // retrieve data from database foreach stores (do not load the collection !)
        $collection = $this->collectionFactory->create();
        $collection->addFieldToFilter('type_id', 'simple')
            ->addFieldToSelect(['sku', 'description', 'created_at'])
            ->setStoreId($store->getId())
            ->addStoreFilter($store->getId())
            ->distinct(true);
            
        // handle pagination system to avoid memory leak
        $currentPage = 1;
        $pageSize = 1000;
        $collection->setPageSize($pageSize);
        $totalPages = $collection->getLastPageNumber();
        while ($currentPage <= $totalPages) {
            $collection->clear();
            $collection->setCurPage($currentPage);
            foreach ($collection->getItems() as $product) {
                // do your business logic here
            }
            $currentPage++;
        }
    };
    
    // your collection will be processed foreach store by a dedicated child process
    $this->parallelStoreProcessor->process(
        $callback,
        $maxChildrenProcess = null,
        $onlyActiveStores = true,
        $withDefaultStore = false
    );
}
```

#### bin/magento thread:processor command

This command allows running a command indefinitely in a dedicated thread using 
the Process Symfony Component.
```php
bin/magento thread:processor <command_name> [command_args...] [--timeout=<timeout>] [--iterations=<iterations>] [--delay=<delay>] [--environment=<environment>] [--progress] [--fail-on-loop] [--ignore-exit-code]
```

##### Options

_(README truncated for .md surface. Full README on https://packagento.com/zepgram/module-multi-threading.)_

## Changelog

All notable changes to this project will be documented in this file.

### [0.3.0] - 2026-02-07

#### Fixed

- **thread:processor exit status masking**: command now returns failure when at least one wrapped iteration fails.
- **thread:processor argument passthrough**: command now supports command arguments (including whitespace command strings like `"help cache:clean"`).
- **thread:processor memory pressure on output-heavy commands**: output is now flushed incrementally while child process is running instead of only at process end.
- **invalid max children configuration**: `ForkedProcessorRunner`, `ParallelStoreProcessor`, and `ParallelWebsiteProcessor` now reject `maxChildrenProcess <= 0`.
- **dimension processors with empty inputs**: store/website processors now return early without running the forked runner when no targets exist.

#### Added

- `thread:processor --fail-on-loop` option to break iteration loop after first failure.
- `thread:processor --ignore-exit-code` option to force success exit code while emitting a warning summary.
- `thread:processor command_args` array argument.

#### Changed

- `ForkedProcessor` fallback now targets explicitly failed pages instead of all non-completed pages.
- `ForkedProcessor` supports configurable child DB reconnect behavior through constructor argument `reconnectDatabaseInChild` (now opt-in; default `false` to preserve DB session compatibility for temporary-table-based workloads).
- `ForkedProcessor` compatibility mode (default) now terminates child workers with signals to avoid PHP child shutdown closing parent DB session state used by temporary-table-based workloads.

### [0.2.0] - 2026-01-26

#### Fixed
- Fix incorrect exit status check that caused false error logs
- Enable `pcntl_async_signals(true)` for proper signal handling
- Replace unreliable self-SIGKILL shutdown with proper exit codes
- Add `pcntl_wait` return value check to prevent infinite loops
- Fix typo `isIdemPotent` -> `isIdempotent` in interface
- Fix `@inheirtDoc` -> `@inheritDoc` typos

## Recent Versions

| Version | Released |
|---|---|
| 0.3.0 | 2026-02-08 |
| 0.1.9 | 2025-07-19 |
| 0.1.8 | 2025-07-09 |
| 0.1.7 | 2025-07-09 |
| 0.1.6 | 2023-12-13 |
| 0.1.5 | 2023-12-08 |
| 0.1.4 | 2023-10-16 |
| 0.1.3 | 2023-04-11 |
| 0.1.2 | 2023-03-07 |
| 0.1.1 | 2023-02-28 |

Showing 10 of 11 versions. Full release history on https://packagento.com/zepgram/module-multi-threading.

## Dependencies

### Require

| Package | Constraint |
|---|---|
| ext-pcntl | * |
| ext-posix | * |
| magento/framework | ^101.0.0\|^102.0.0\|^103.0.0 |
| magento/module-store | ^101 |

## Quality

Latest release (0.3.0) fails the Packagento QA pipeline. Verdicts below are per-cell (Magento line × PHP version) for the matrixed tools, and run-once for the static / security tiers.


### Compatibility

Each Magento line is installed on its supported PHP versions, then the module is built (DI compile + static-content deploy). Cells show passed / failed / untested; staircase gaps render as `–`.

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | Pass | Pass | – | – |
| 2.4.8 | – | Pass | Pass | – |
| 2.4.9 | – | – | 1 | 1 |


### Code Quality

Advisory checks against the module's source. Never affect the Compatibility verdict — a phpcs finding can't make a module incompatible.

#### Static Analysis

Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.

| Tool | Status | Findings | Summary |
|---|---|---|---|
| PHPCS | Fail | 49 | 1 error, 48 warnings (ruleset: Magento2) — 8 auto-fixable with phpcbf |
| PHPMD | Warning | 14 | 14 rule violations (CyclomaticComplexity:3, MissingImport:3, NPathComplexity:2, ExcessiveMethodLength:2, UnusedFormalParameter:2) |
| Cpd | Pass | 0 |  |
| Composer validate | Info | 1 | valid; 1 advisory note (composer validate --strict) |

#### PHPStan

Type-checks the module against a real Magento install. Re-runs per Magento + PHP version because resolvable symbols differ between releases.

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | 9 | 9 | – | – |
| 2.4.8 | – | 9 | 9 | – |
| 2.4.9 | – | – | 12 | 12 |


### Tests

Unit and integration suites run per Magento + PHP cell. Test failures speak to the module's behaviour, not its compatibility with a line, so they're reported here separately.

#### Unit Tests

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | – | – |
| 2.4.8 | – | N/A | N/A | – |
| 2.4.9 | – | – | N/A | N/A |

#### Integration Tests

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | – | – |
| 2.4.8 | – | N/A | N/A | – |
| 2.4.9 | – | – | N/A | N/A |


### Security

Dependency-advisory audit (composer audit) plus a source malware scan. A malware detection fails the version outright.

| Tool | Status | Findings | Summary |
|---|---|---|---|
| Composer audit | Pass | 0 |  |
| Malware scan | Pass | 0 |  |

## Licence and pricing

Free. A licence is still minted on checkout and bound to your project for Composer access — no payment step.

Refundable within 14 days of first purchase via https://packagento.com/account/refunds/.

## Install via Claude Code or any MCP client

The Packagento MCP server can run the licence + project + Composer steps above in one tool call:

```
purchase_and_install_packages(
  composer_names=["zepgram/module-multi-threading"],
  project_id="proj_xxx"
)
```

This handles cart, checkout, licence minting, project activation, and writes auth.json credentials. Connect a client with `claude mcp add packagento https://mcp.packagento.com`. Full setup at https://packagento.com/docs/mcp-setup.

## Vendor

zepgram is a Magento 2 vendor on Packagento. See https://packagento.com/zepgram.md for their full catalogue.

