8000 GitHub - lnlingyuan/process: :whale2: The process wrapper and manager based on PCNTL on the Unix-like systems using php
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
forked from slince/process

🐋 The process wrapper and manager based on PCNTL on the Unix-like systems using php

Notifications You must be signed in to change notification settings

lnlingyuan/process

 
 

Repository files navigation

Process library

Build Status Coverage Status Latest Stable Version Scrutinizer

The library help to work with processes. It provides a more readable api and various modes for IPC via pipe(FIFO) and system v.

Installation

Install via composer

composer require slince/process

Dependencies

The library replies on the following php's extension.

  • ext-pcntl. Provides control processes (MUST)
  • ext-sysvshm. Porvides system v shared memory (OPTIONAL)
  • ext-sysvsem. Porvides system v semaphore (OPTIONAL)
  • ext-sysmsg. Porvides system v message queue (OPTIONAL)

Usage

Basic usage

$process = new Slince\Process\Process(function(){
    echo 'hello, my pid is ' . getmypid();
});
$process->start();

var_dump($process->isRunning()); // echo true
var_dump($process->getPid()); // will output the pid of child process
//do something other

$process->wait(); //waiting for the process to exit 

Sends signal to the process

Note: If your php version is less than 7.1, please add the statement declare(ticks=1); at the beginning of the file:

$process = new Slince\Process\Process(function(){
    echo 'hello, my pid is ' . getmypid();
});
$process->getSignalHandler()->register([SIGUSR1, SIGUSR2], function(){
    echo 'trigger signal';
});
$process->start();
$process->signal(SIGUSER1);
//do something
$process->wait();

Shared memory

$memory = new Slince\Process\SystemV\SharedMemory();
$memory->set('foo', 'bar');
var_dump($memory->get('foo'));

The default size of shared memory is the sysvshm.init_mem in the php.ini, otherwise 10000 bytes. You can adjust this.

$memory = new Slince\Process\SystemV\SharedMemory(__FILE__, '5M'); //Adjusts to 5m

Semaphore

$semaphore = new Slince\Process\SystemV\Semaphore();
$semaphore->acquire(); //Acquires a lock
// do something
$semaphore->release() //Releases a lock

Message queue

$queue  = new Slince\Process\SystemV\MessageQueue();
$queue->send('hello');
echo $queue->receive(); //Will output hello

Fifo

$writeFifo = new Slince\Process\Pipe\WritableFifo('/tmp/test.pipe');
$writeFifo->write('some message');
$readFifo = new Slince\Process\Pipe\ReadableFifo('/tmp/test.pipe');
echo $readFifo->read();

Fifo works with half duplex mode. You can use DuplexFifo that will create two fifos.

$fifo = new Slince\Process\Pipe\DuplexFifo('/tmp/test.pipe');
$fifo->write('some message');
$fifo->read();

About

🐋 The process wrapper and manager based on PCNTL on the Unix-like systems using php

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%
0