Skip to content

Add multi-callback worker pool API to ForkManager #1

@coisa

Description

@coisa

Context

The current ForkManagerInterface::fork(callable $workerCallback, int $workerCount = 1) (and implementation) only accepts a single callback and spawns N workers for that same callback.

This is limiting:

  • we can only scale the same unit of work;
  • there is no way to define different routines per worker in the same call.

Why this matters

In practice, if a caller needs worker types like:

  • one process validating input,
  • one process doing IO-bound work,
  • one process handling notifications,

it currently requires multiple fork() calls and orchestration outside the component, or custom dispatch logic.

Proposed change

Add support for multi-callback worker pools so one fork orchestration can map each callback independently.

Recommended API (non-breaking first)

Keep existing behavior and add:

  • forkMany(array $workerCallbacks) to dispatch a heterogeneous callback set.

Proposed behavior:

  • key/index in the array identifies each worker callback task;
  • each callback receives the same shared context as today (or explicit context strategy if needed);
  • result/exception collection remains deterministic per worker.

Alternative (possibly breaking)

Replace fork(callable $workerCallback, int $workerCount = 1) with a variadic/callback-list signature.

Because this is BC-sensitive, this should be considered carefully.

Non-goals

  • changing the fork strategy/scheduler itself;
  • adding process-pool abstractions or advanced load-balancing in this first iteration.

Acceptance criteria

  • New tests covering:
    • mixed callbacks executed in the same fork invocation;
    • worker count aligned with callback list length;
    • independent callback failure reporting.
  • Keep old fork(callable, int) behavior untouched when using the legacy API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions