Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ParallelExecutor.php
1<?php
2declare( strict_types = 1 );
3
5
6use MediaWiki\MediaWikiServices;
7
19 private $pids = [];
20
21 public function __construct(
22 private readonly int $threads = 1,
23 ) {
24 }
25
26 public function runInParallel( callable $mainThread, callable $forkThread ): void {
27 // Fork to increase speed with parallelism. Also helps with memory usage if there are leaks.
28 $pid = -1;
29 if ( function_exists( 'pcntl_fork' ) ) {
30 $pid = pcntl_fork();
31 }
32
33 if ( $pid === 0 ) {
34 MediaWikiServices::resetChildProcessServices();
35 $forkThread();
36 exit();
37 } elseif ( $pid === -1 ) {
38 // Fork failed do it serialized
39 $forkThread();
40 } else {
41 // Main thread
42 $mainThread( $pid );
43 $this->pids[$pid] = true;
44
45 // If we hit the thread limit, wait for any child to finish.
46 if ( count( $this->pids ) >= $this->threads ) {
47 $status = 0;
48 $pid = pcntl_wait( $status );
49 unset( $this->pids[$pid] );
50 }
51 }
52 }
53}
Helper class for maintenance scripts to run things in parallel.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30