Giter VIP home page Giter VIP logo

multiprocess's Introduction

PHP Mutilprocess

This is high performance PHP Mutilprocessing Task Manager written in PHP, compatible with PHP-FPM and CLI.
这是一个用PHP编写的高性能PHP多进程异步任务管理器,与PHP FPM和CLI兼容。

Contents

Feature

  • By using this tool, PHP scripts can be invoked asynchronously based on multi processes, and finally wait for each process to return results, saving lots of time.

  • graceful and efficient

  • can get callable function return value

  • You can specify the PHP path for asynchronous execution scripts by specifying the phpBin parameter.

  • Exception Handling

Installation

You can use composer to install this library from the command line.

composer require sinacms/multiprocess

Usage

distribute tasks and async execute

<?php

use Mutilprocessing\Async;

Async::create()->start('task.php', ['runTest'.$i]);

distribute tasks by a simple function and async execute

!!! WARN:please don't insert a '&&&' string in echo and return for some reason it will break down the program run

<?php

use Mutilprocessing\Async;

Async::create()->startFunc(function($param1, $param2) {
    echo $param1.$param2.PHP_EOL;
}, ['param1' => 'hello', 'param2' => ' world']);

$func = function ($param1, $param2) {
    echo "this is an another func";
};

Async::create()->startFunc($func, ['param1' => 'hello', 'param2' => ' PHP']); 

sync wait for all process end

<?php

use Mutilprocessing\Async;
$outData = [];
Async::wait(function($code, $out, $err) use(&$outData) {
//    var_dump($code, $out, $err);
//  you can handle code runtime exception like this
	 if (strlen($err) != 0) {
	 	//  do sth.
	 }
	 // and you can get return value like this
	 // more function detail see examples :)
	 array_push($outData, \Mutilprocessing\Async::getReturn($out));
});

outData structure:

echos represent echos in execute

returns represent return in execute

array(4) {
  [0] =>
  array(2) {
    'echos' =>
    string(5) "hello"
    'returns' =>
    string(0) ""
  }
  [1] =>
  array(2) {
    'echos' =>
    string(6) "KZ RNG"
    'returns' =>
    string(10) "return 777"
  }
  [2] =>
  array(2) {
    'echos' =>
    string(17) "EDG AFSreturn 888"
    'returns' =>
    string(0) ""
  }
  [3] =>
  array(2) {
    'echos' =>
    string(6) "SKT RW"
    'returns' =>
    string(10) "return 666"
  }
}

getArgs passed from Async::start

// please pass $argv[1] to get args
<?php

use Mutilprocessing\Async;

Async::getArgs($argv[1], 'key');

clean all task

<?php

use Mutilprocessing\Async;

Async::discard();

Documentation

  • Async

  • option shorthand

  • public static function create()
  • public static function start($scriptname, $args, $phpBin="", $envs = [])
  • public function startFunc(callable $function, $args = [], $phpBin="")
  • public static function discard()
  • public static function wait(callable $logHandler = null)
  • public static function getArgs($argv = null)
  • public static function getReturn($out)
  • FunctionParser

  • option shorthand

  • public static function genTmp(callable $function)

Todo

  • regCallback for each execution (on process)
  • add multi execution unit and start once

multiprocess's People

Contributors

sunsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.