PHP Classes

Threadator: Create threads and send messages between them

Recommend this page to a friend!
  Info   View files View files (109)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (2 months ago) RSS 2.0 feedNot enough user ratingsTotal: 390 All time: 6,642 This week: 188Up
Version License PHP version Categories
threadator 0.0.12Custom (specified...5.5PHP 5, Unix, Language
Description 

Author

This package can create threads and send messages between them.

It can create a parallel process and execute PHP code of a given callback function on that process.

It can also create mutexes that can be used to guarantee that only on process runs certain critical section code.

The processes may communicate sending and receiving messages between the master process and child processes. The communication may be implemented with different drives to use for instance message queues or Redis.

Picture of AlexanderC
  Performance   Level  
Name: AlexanderC is available for providing paid consulting. Contact AlexanderC .
Classes: 13 packages by
Country: Moldova Moldova
Age: 34
All time rank: 7301 in Moldova Moldova
Week rank: 312 Up1 in Moldova Moldova Equal
Innovation award
Innovation award
Nominee: 3x

Details

# Main Goal The mail goal of [Threadator](https://github.com/AlexanderC/Threadator) package is to provide an easy way to run multithreaded applications in PHP. You may notice that there are other packages like this, but: - This is a modern package (Using Generators, Traits and other language sugar + build as a composer library) - It is providing most native implementation ever (all that you need are posix, pcntl) - You have full controll on your threads (Mutex and bidirectional Communication betweed threads and master) # Installation - Via [Composer](https://getcomposer.org/) "alexanderc/threadator": "dev-master" # Basic usage ```php <?php require '/path/to/vendor/autoload.php'; $runtime = new \Threadator\Runtime(); $factory = new \Threadator\Factory($runtime); // instead of built in drivers names ["msgQueue", "redis"] you can use your own driver // by providing fully qualified class name (with NS), like My\CommunicationDriver\TestDriver // IMPORTANT: in order to get a stable work of communication- use "redis" instead of "msgQueue" $communication = \Threadator\Communication\Communication::create($runtime, 'msgQueue' /* 'redis', ['127.0.0.1'] */ ); $runtime->setCommunication($communication); // now we can create some threads for($i = 0; $i < 5; $i++) { // you can also use your own thread implementation by calling $factory->create("You\\Thread\\Implementation\\TestThread"); // IMPORTANT: your TestThread class should extend Threadator\Thread /** @var \Threadator\Thread $thread */ $thread = $factory->createCallable(function($thread) { // create mutex // for more mutex types check \Threadator\Mutex::T_* constants $mutex = $thread->createMutex("echo", \Threadator\Mutex::T_FUNCTION); // wait until we aquire the mutex $mutex->waitAcquire(); // do some work here... sleep(mt_rand(1, 3)); echo "Running Thread #{$thread->getPid()}...\n"; // wait until we get a message from the main process $thread->receiveMessage($message); // send back this message $thread->sendMessage("#{$thread->getPid()}: {$message}"); }); } echo "Main process #{$runtime->getPid()} running!\n"; // start all threads $runtime->run(); // send a message to all threads foreach($runtime->broadcastMessage(microtime(true)) as list($result, $thread)) { // if result == 1 than message was sent echo "Result for msg #{$thread->getPid()} -> {$result}\n"; } // wait until we receive thread messages $messages = []; foreach($runtime->receiveMessage() as $result => $message) { if($result) { $messages[] = $message; } } echo "Thread messages: " . implode(", ", $messages) . "\n"; // wait until all the threads runs $runtime->join(); exit("Main process #{$runtime->getPid()} stopped!\n"); ``` For more examples check "test" folder # Is it extendable Yes it is! You can easily write your communication driver or an thread implementation. # How to extend If you need to add (for example) a new communication driver- you just need to extend Threadator\Communication\Driver like this ```php <?php /** * @author AlexanderC <self@alexanderc.me> * @date 4/10/14 * @time 2:04 PM */ use Threadator\Communication\Driver\ADriver; class TestDriver extends ADriver { /** * @return void */ protected function init() { // TODO: Implement init() method. } /** * @param int $key * @param mixed $message * @return bool */ public function send($key, $message) { // TODO: Implement send() method. } /** * Try to get message, but do not block * * @param int $key * @param mixed $message * @return bool */ public function touch($key, & $message) { // TODO: Implement touch() method. } /** * Block until the first message arrives * * @param int $key * @param mixed $message * @return bool */ public function receive($key, & $message) { // TODO: Implement receive() method. } } ``` # ToDo - Create unit tests - Implement more communication drivers - ...your suggestions # Contributors - [AlexanderC](mailto:self@alexanderc.me) - ...[all contributors](https://github.com/AlexanderC/Threadator/graphs/contributors)

  Files folder image Files  
File Role Description
Files folder imagedocs (1 directory)
Files folder imagesrc (13 files, 2 directories)
Files folder imagetest (4 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  docs  
File Role Description
Files folder imageapi (1 file, 8 directories)

  Files folder image Files  /  docs  /  api  
File Role Description
Files folder imageclasses (12 files)
Files folder imagecss (3 files, 1 directory)
Files folder imagefiles (20 files, 2 directories)
Files folder imagegraphs (1 file)
Files folder imageimages (8 files, 1 directory)
Files folder imagejs (6 files)
Files folder imagenamespaces (5 files)
Files folder imagereports (3 files)
  Accessible without login HTML file index.html Doc. Documentation

  Files folder image Files  /  docs  /  api  /  classes  
File Role Description
  Accessible without login HTML file Threadator.Communi....Communication.html Doc. Documentation
  Accessible without login HTML file Threadator.Communi...Driver.ADriver.html Doc. Documentation
  Accessible without login HTML file Threadator.Communi...river.MsgQueue.html Doc. Documentation
  Accessible without login HTML file Threadator.Communi...n.Driver.Redis.html Doc. Documentation
  Accessible without login HTML file Threadator.Communi...ationConstants.html Doc. Documentation
  Accessible without login HTML file Threadator.Communi...dCommunication.html Doc. Documentation
  Accessible without login HTML file Threadator.Factory.html Doc. Documentation
  Accessible without login HTML file Threadator.Impleme...CallableThread.html Doc. Documentation
  Accessible without login HTML file Threadator.IMutexType.html Doc. Documentation
  Accessible without login HTML file Threadator.IThreadState.html Doc. Documentation
  Accessible without login HTML file Threadator.Mutex.html Doc. Documentation
  Accessible without login HTML file Threadator.Thread.html Doc. Documentation

  Files folder image Files  /  docs  /  api  /  css  
File Role Description
Files folder imagephpdocumentor-clean-icons (3 files, 1 directory)
  Accessible without login Plain text file jquery.iviewer.css Data Auxiliary data
  Accessible without login Plain text file prism.css Data Auxiliary data
  Accessible without login Plain text file template.css Data Auxiliary data

  Files folder image Files  /  docs  /  api  /  css  /  phpdocumentor-clean-icons  
File Role Description
Files folder imagefonts (2 files)
  Accessible without login Plain text file lte-ie7.js Data Auxiliary data
  Accessible without login Plain text file Read Me.txt Doc. Documentation
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files  /  docs  /  api  /  css  /  phpdocumentor-clean-icons  /  fonts  
File Role Description
  Accessible without login Plain text file phpdocumentor-clean-icons.dev.svg Data Auxiliary data
  Accessible without login Plain text file phpdocumentor-clean-icons.svg Data Auxiliary data

  Files folder image Files  /  docs  /  api  /  files  
File Role Description
Files folder imageCommunication (4 files, 1 directory)
Files folder imageImplementation (1 file)
  Accessible without login HTML file Communication.Communication.php.html Doc. Documentation
  Accessible without login HTML file Communication.Driver.ADriver.php.html Doc. Documentation
  Accessible without login HTML file Communication.Driver.MsgQueue.php.html Doc. Documentation
  Accessible without login HTML file Communication.Driver.Redis.php.html Doc. Documentation
  Accessible without login HTML file Communication.ICom...nConstants.php.html Doc. Documentation
  Accessible without login HTML file Communication.TRun...munication.php.html Doc. Documentation
  Accessible without login HTML file Communication.TThr...munication.php.html Doc. Documentation
  Accessible without login HTML file Factory.php.html Doc. Documentation
  Accessible without login Plain text file Factory.php.txt Doc. Documentation
  Accessible without login HTML file Implementation.CallableThread.php.html Doc. Documentation
  Accessible without login HTML file IMutexType.php.html Doc. Documentation
  Accessible without login Plain text file IMutexType.php.txt Doc. Documentation
  Accessible without login HTML file IThreadState.php.html Doc. Documentation
  Accessible without login Plain text file IThreadState.php.txt Doc. Documentation
  Accessible without login HTML file Mutex.php.html Doc. Documentation
  Accessible without login Plain text file Mutex.php.txt Doc. Documentation
  Accessible without login HTML file Runtime.php.html Doc. Documentation
  Accessible without login Plain text file Runtime.php.txt Doc. Documentation
  Accessible without login HTML file Thread.php.html Doc. Documentation
  Accessible without login Plain text file Thread.php.txt Doc. Documentation

  Files folder image Files  /  docs  /  api  /  files  /  Communication  
File Role Description
Files folder imageDriver (3 files)
  Accessible without login Plain text file Communication.php.txt Doc. Documentation
  Accessible without login Plain text file ICommunicationConstants.php.txt Doc. Documentation
  Accessible without login Plain text file TRuntimeCommunication.php.txt Doc. Documentation
  Accessible without login Plain text file TThreadCommunication.php.txt Doc. Documentation

  Files folder image Files  /  docs  /  api  /  files  /  Communication  /  Driver  
File Role Description
  Accessible without login Plain text file ADriver.php.txt Doc. Documentation
  Accessible without login Plain text file MsgQueue.php.txt Doc. Documentation
  Accessible without login Plain text file Redis.php.txt Doc. Documentation

  Files folder image Files  /  docs  /  api  /  files  /  Implementation  
File Role Description
  Accessible without login Plain text file CallableThread.php.txt Doc. Documentation

  Files folder image Files  /  docs  /  api  /  graphs  
File Role Description
  Accessible without login HTML file class.html Doc. Documentation

  Files folder image Files  /  docs  /  api  /  images  
File Role Description
Files folder imageiviewer (8 files)
  Accessible without login Plain text file custom-icons.svg Data Auxiliary data
  Accessible without login Image file hierarchy-item.png Icon Icon image
  Accessible without login Image file icon-class-13x13.png Icon Icon image
  Accessible without login Plain text file icon-class.svg Data Auxiliary data
  Accessible without login Image file icon-interface-13x13.png Icon Icon image
  Accessible without login Plain text file icon-interface.svg Data Auxiliary data
  Accessible without login Image file icon-trait-13x13.png Icon Icon image
  Accessible without login Plain text file icon-trait.svg Data Auxiliary data

  Files folder image Files  /  docs  /  api  /  images  /  iviewer  
File Role Description
  Accessible without login Image file grab.cur Data Auxiliary data
  Accessible without login Image file hand.cur Data Auxiliary data
  Accessible without login Image file iviewer.rotate_left.png Icon Icon image
  Accessible without login Image file iviewer.rotate_right.png Icon Icon image
  Accessible without login Image file iviewer.zoom_fit.png Icon Icon image
  Accessible without login Image file iviewer.zoom_in.png Icon Icon image
  Accessible without login Image file iviewer.zoom_out.png Icon Icon image
  Accessible without login Image file iviewer.zoom_zero.png Icon Icon image

  Files folder image Files  /  docs  /  api  /  js  
File Role Description
  Accessible without login Plain text file jquery.dotdotdot-1.5.9.js Data Auxiliary data
  Accessible without login Plain text file jquery.dotdotdot-1.5.9.min.js Data Auxiliary data
  Accessible without login Plain text file jquery.iviewer.js Data Auxiliary data
  Accessible without login Plain text file jquery.iviewer.min.js Data Auxiliary data
  Accessible without login Plain text file jquery.smooth-scroll.js Data Auxiliary data
  Accessible without login Plain text file prism.min.js Data Auxiliary data

  Files folder image Files  /  docs  /  api  /  namespaces  
File Role Description
  Accessible without login HTML file default.html Doc. Documentation
  Accessible without login HTML file Threadator.Communication.Driver.html Doc. Documentation
  Accessible without login HTML file Threadator.Communication.html Doc. Documentation
  Accessible without login HTML file Threadator.html Doc. Documentation
  Accessible without login HTML file Threadator.Implementation.html Doc. Documentation

  Files folder image Files  /  docs  /  api  /  reports  
File Role Description
  Accessible without login HTML file deprecated.html Doc. Documentation
  Accessible without login HTML file errors.html Doc. Documentation
  Accessible without login HTML file markers.html Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageCommunication (4 files, 1 directory)
Files folder imageImplementation (1 file)
  Plain text file BalanceAwareRuntime.php Class Class source
  Plain text file Factory.php Class Class source
  Plain text file IMutexType.php Class Class source
  Plain text file IThreadState.php Class Class source
  Plain text file Mutex.php Class Class source
  Plain text file Runtime.php Class Class source
  Plain text file TBalanceAwareRuntimeProperties.php Class Class source
  Plain text file Thread.php Class Class source
  Plain text file TRuntimeControl.php Class Class source
  Plain text file TRuntimeProperties.php Class Class source
  Plain text file TThreadControl.php Class Class source
  Plain text file TThreadMutex.php Class Class source
  Plain text file TThreadProperties.php Class Class source

  Files folder image Files  /  src  /  Communication  
File Role Description
Files folder imageDriver (3 files)
  Plain text file Communication.php Class Class source
  Plain text file ICommunicationConstants.php Class Class source
  Plain text file TRuntimeCommunication.php Class Class source
  Plain text file TThreadCommunication.php Class Class source

  Files folder image Files  /  src  /  Communication  /  Driver  
File Role Description
  Plain text file ADriver.php Class Class source
  Plain text file MsgQueue.php Class Class source
  Plain text file Redis.php Class Class source

  Files folder image Files  /  src  /  Implementation  
File Role Description
  Plain text file CallableThread.php Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imageclasses (1 file)
  Accessible without login Plain text file balanced.php Test Unit test script
  Accessible without login Plain text file basic.php Test Unit test script
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Accessible without login Plain text file class.php Test Unit test script

  Files folder image Files  /  test  /  classes  
File Role Description
  Accessible without login Plain text file EchoThread.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:390
This week:0
All time:6,642
This week:188Up