Skip to content

xujiajun/parellel-tasks

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP parallel tasks

## Install It's simple! Just use composer commands

composer require 'aoj/parallel-tasks:~1.0'

Usage

<?php

use AoJ\ParallelTasks\Task;
use AoJ\ParallelTasks\TaskRunner;

$tasks = [
	new Task("sleep " . rand(1, 5)),
	new Task("sleep " . rand(1, 5)),
	new Task("sleep " . rand(1, 5)),
	new Task("sleep " . rand(1, 5)),
];

$taskRunner = new TaskRunner($tasks, 2);
$taskRunner->run();

foreach($tasks as $task) {
	var_dump($task->getResult());
}

or try

php example.php

Methods

TaskRunner(Task[], $parallel = 3)

  • run() start tasks
  • setParallel($newParallelCount) set number of simultaneously process
  • <integer> getParallel()

### Task($cmd, $args = array(), $cwp = DIR)

  • run() start process
  • <bool> isRunning() check if process finished. You must run it before getResult()
  • <string> getResult() return stdout of process. Returns data continuously when it appears at stdout
  • <integer|null> getExitCode() returns exit code of process. Returns null if the process is still running or not yet started
  • <integer> getDuration() Returns duration time in milliseconds, returns 0 if process not yet started
  • <integer> getPid() Returns process pid. Returns 0 if process not yet started
  • <string> getCmd() Returns command with arguments and without cwd

## TODO

  • add tests, more tests
  • update readme (see example.php for now)
  • update

About

Run parallel process with php

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%