Git Product home page Git Product logo

daemon's Introduction

Background process execution.

Execute background processes asynchronously using an object oriented process pool.


Build status Code quality Code coverage Current version PHP.G/Daemon documentation

Example usage

<?php
use Gt\Daemon\Process;
use Gt\Daemon\Pool;

// Create three long-running processes:
$pingProcess = new Process("ping google.com");
$infiniteProcess = new Process("while true; do echo 'background...'; sleep 3; done");
$dateProcess = new Process("while true; do echo $(date -d now); sleep 2; done");

// Add all three processes to a pool:
$pool = new Pool();
$pool->add("Ping", $pingProcess);
$pool->add("Loop", $infiniteProcess);
$pool->add("Date", $dateProcess);

// Start the execution of all processes:
$pool->exec();

// While processes are running, write their output to the terminal:
do {
	echo $pool->read();
	// Sleep to avoid hogging the CPU.
	sleep(1);
}
while($pool->numRunning() > 0);

Outputs something similar to:

[Ping] PING google.com (172.217.169.78) 56(84) bytes of data.
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=1 ttl=52 time=8.78 ms
[Loop] background...
[Date] Mon 19 Aug 09:58:54 BST 2019
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=2 ttl=52 time=8.75 ms
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=3 ttl=52 time=8.75 ms
[Date] Mon 19 Aug 09:58:56 BST 2019
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=4 ttl=52 time=8.75 ms
[Loop] background...
[Ping] 64 bytes from lhr48s09-in-f14.1e100.net (172.217.169.78): icmp_seq=5 ttl=52 time=8.80 ms
[Date] Mon 19 Aug 09:58:58 BST 2019

Notice how the date process is only set to loop three times, and after it is complete the other two infinite processes continue to run.

daemon's People

Contributors

g105b avatar sayyassine9 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.