Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4153776
ParallelExecutor.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
ParallelExecutor.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
MediaWiki\Extension\Translate\Utilities
;
use
MediaWiki\MediaWikiServices
;
/**
* Helper class for maintenance scripts to run things in parallel.
*
* See also ForkContoller and https://phabricator.wikimedia.org/T201970.
*
* @since 2020.11
* @license GPL-2.0-or-later
* @author Niklas Laxström
*/
class
ParallelExecutor
{
/** @var true[] */
private
$pids
=
[];
/** @var int */
private
$threads
;
public
function
__construct
(
int
$threads
=
1
)
{
$this
->
threads
=
$threads
;
}
public
function
runInParallel
(
callable
$mainThread
,
callable
$forkThread
):
void
{
// Fork to increase speed with parallelism. Also helps with memory usage if there are leaks.
$pid
=
-
1
;
if
(
function_exists
(
'pcntl_fork'
)
)
{
$pid
=
pcntl_fork
();
}
if
(
$pid
===
0
)
{
MediaWikiServices
::
resetChildProcessServices
();
$forkThread
();
exit
();
}
elseif
(
$pid
===
-
1
)
{
// Fork failed do it serialized
$forkThread
();
}
else
{
// Main thread
$mainThread
(
$pid
);
$this
->
pids
[
$pid
]
=
true
;
// If we hit the thread limit, wait for any child to finish.
if
(
count
(
$this
->
pids
)
>=
$this
->
threads
)
{
$status
=
0
;
$pid
=
pcntl_wait
(
$status
);
unset
(
$this
->
pids
[
$pid
]
);
}
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Aug 19 2026, 20:51 (7 w, 5 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
20/77/8e1c6b047032ab8a15ad11cbdf32
Default Alt Text
ParallelExecutor.php (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment