Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use child processes even if number of processes is only one #15

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Parallelization.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,12 @@ protected function executeMasterProcess(InputInterface $input, OutputInterface $
{
$this->runBeforeFirstCommand($input, $output);

$numberOfProcessesDefined = null !== $input->getOption('processes');
$numberOfProcesses = (int) $input->getOption('processes');
$hasItem = (bool) $input->getArgument('item');
$items = $hasItem ? [$input->getArgument('item')] : $this->fetchItems($input);
$count = count($items);
$segmentSize = 1 === $numberOfProcesses ? $count : $this->getSegmentSize();
$segmentSize = 1 === $numberOfProcesses && !$numberOfProcessesDefined ? $count : $this->getSegmentSize();
$batchSize = $this->getBatchSize();
$rounds = 1 === $numberOfProcesses ? 1 : ceil($count * 1.0 / $segmentSize);
$batches = ceil($segmentSize * 1.0 / $batchSize) * $rounds;
Expand Down Expand Up @@ -347,7 +348,7 @@ protected function executeMasterProcess(InputInterface $input, OutputInterface $
$progressBar->setFormat('debug');
$progressBar->start();

if ($count <= $segmentSize || 1 === $numberOfProcesses) {
if ($count <= $segmentSize || (1 === $numberOfProcesses && !$numberOfProcessesDefined)) {
// Run in the master process

$itemsChunks = array_chunk(
Expand Down
30 changes: 29 additions & 1 deletion tests/ParallelizationIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function test_it_can_run_the_command_without_sub_processes(): void

$this->assertSame(
<<<'EOF'
Processing 2 movies in segments of 2, batches of 50, 1 round, 1 batches in 1 process
Processing 2 movies in segments of 50, batches of 50, 1 round, 1 batches in 1 process

0/2 [>---------------------------] 0% < 1 sec/< 1 sec 10.0 MiB
2/2 [============================] 100% < 1 sec/< 1 sec 10.0 MiB
Expand Down Expand Up @@ -112,6 +112,34 @@ public function test_it_can_run_the_command_with_multiple_processes(): void

Processed 2 movies.

EOF
,
$actual,
'Expected logs to be identical'
);
}

public function test_it_can_run_the_command_with_one_process_as_child_process(): void
{
$this->commandTester->execute(
[
'command' => 'import:movies',
'--processes' => 1,
],
['interactive' => true]
);

$actual = $this->getOutput();

$this->assertSame(
<<<'EOF'
Processing 2 movies in segments of 50, batches of 50, 1 round, 1 batches in 1 process

0/2 [>---------------------------] 0% < 1 sec/< 1 sec 10.0 MiB
2/2 [============================] 100% < 1 sec/< 1 sec 10.0 MiB

Processed 2 movies.

EOF
,
$actual,
Expand Down