Skip to content

Commit 8f7440d

Browse files
Use child processes even if number of processes is only one (#15)
1 parent 7878e88 commit 8f7440d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/Parallelization.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,12 @@ protected function executeMasterProcess(InputInterface $input, OutputInterface $
295295
{
296296
$this->runBeforeFirstCommand($input, $output);
297297

298+
$numberOfProcessesDefined = null !== $input->getOption('processes');
298299
$numberOfProcesses = (int) $input->getOption('processes');
299300
$hasItem = (bool) $input->getArgument('item');
300301
$items = $hasItem ? [$input->getArgument('item')] : $this->fetchItems($input);
301302
$count = count($items);
302-
$segmentSize = 1 === $numberOfProcesses ? $count : $this->getSegmentSize();
303+
$segmentSize = 1 === $numberOfProcesses && !$numberOfProcessesDefined ? $count : $this->getSegmentSize();
303304
$batchSize = $this->getBatchSize();
304305
$rounds = 1 === $numberOfProcesses ? 1 : ceil($count * 1.0 / $segmentSize);
305306
$batches = ceil($segmentSize * 1.0 / $batchSize) * $rounds;
@@ -347,7 +348,7 @@ protected function executeMasterProcess(InputInterface $input, OutputInterface $
347348
$progressBar->setFormat('debug');
348349
$progressBar->start();
349350

350-
if ($count <= $segmentSize || 1 === $numberOfProcesses) {
351+
if ($count <= $segmentSize || (1 === $numberOfProcesses && !$numberOfProcessesDefined)) {
351352
// Run in the master process
352353

353354
$itemsChunks = array_chunk(

tests/ParallelizationIntegrationTest.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function test_it_can_run_the_command_without_sub_processes(): void
7777

7878
$this->assertSame(
7979
<<<'EOF'
80-
Processing 2 movies in segments of 2, batches of 50, 1 round, 1 batches in 1 process
80+
Processing 2 movies in segments of 50, batches of 50, 1 round, 1 batches in 1 process
8181
8282
0/2 [>---------------------------] 0% < 1 sec/< 1 sec 10.0 MiB
8383
2/2 [============================] 100% < 1 sec/< 1 sec 10.0 MiB
@@ -112,6 +112,34 @@ public function test_it_can_run_the_command_with_multiple_processes(): void
112112
113113
Processed 2 movies.
114114

115+
EOF
116+
,
117+
$actual,
118+
'Expected logs to be identical'
119+
);
120+
}
121+
122+
public function test_it_can_run_the_command_with_one_process_as_child_process(): void
123+
{
124+
$this->commandTester->execute(
125+
[
126+
'command' => 'import:movies',
127+
'--processes' => 1,
128+
],
129+
['interactive' => true]
130+
);
131+
132+
$actual = $this->getOutput();
133+
134+
$this->assertSame(
135+
<<<'EOF'
136+
Processing 2 movies in segments of 50, batches of 50, 1 round, 1 batches in 1 process
137+
138+
0/2 [>---------------------------] 0% < 1 sec/< 1 sec 10.0 MiB
139+
2/2 [============================] 100% < 1 sec/< 1 sec 10.0 MiB
140+
141+
Processed 2 movies.
142+
115143
EOF
116144
,
117145
$actual,

0 commit comments

Comments
 (0)