Commit 5d442e0b authored by Jamie Carl's avatar Jamie Carl 🤙🏼

Update README.md

parent 84229c42
# Basic Example
# The simple PHP threading class
This is a simple, lightweight "extension free" way of handling multi-threading
in PHP. When I say "extension free" I mean that no 3rd party PHP extensions
are required, such as [pthreads](https://pecl.php.net/package/pthreads). This
is because it uses the built-in PHP process control functions such as
[proc_open](https://secure.php.net/manual/en/function.proc-open.php) and
[proc_close](https://secure.php.net/manual/en/function.proc-close.php) and
stream read/write functions.
# Requirements
Although this class is provided as part of the Hazaar MVC framework, it does not
actually require Hazaar MVC and can be used on it's own.
The only requirements are:
* PHP 5.x
* Linux.
## Windows?
Windows is probably NOT supported. I dunno. I haven't tested it so probably
not.
# Installation
Installation is done using composer. From your project directory simply:
```
$ composer require hazaarlabs/hazaar-thread
```
and you should be good to go.
# Usage
Using the class is quite simple. From your parent thread, use the
```Thread::Create( file.php )``` method to create a new Thread object. The code in
file.php should be a whole, executable PHP program. You can use the
```Thread::Tell()``` to send data to the threads STDIN stream and
```Thread::Listen()``` to read data from the threads STDOUT stream. Ideally I
should have written another class to handle IO inside the thread itself but my
'worker thread' code uses stream_select to read strings from STDIN and then
fwrite to send data back to STDOUT and the parent process.
## Basic Example
```php
$thread = Thread::Create( 'test.php' );
$thread->Tell('DOSTUFF');
......@@ -13,4 +60,28 @@ while(true){
}
$thread->close();
```
\ No newline at end of file
```
# Why did I create this?
Back in the day, I needed a way to communicate with a server process from a web
interface using nothing but PHP. This was due to some shared code that I did
not want to rewrite in another language that natively supported threads.
The plan was to implement a socket server that listened for connections from the
web interface, then returned the status and allowed control of the server
process. The problem was, while the server process was doing work it would be
unable to serve requests from the web UI without some sort of multi-threading.
This is because running things in a single thread would allow you to run the
schedule OR service an interface request, but not both at the same time.
Therefore I decided to take it upon myself to write a PHP thread class that
makes use of ONLY the PHP process control functions without the need for a 3rd
party PHP extension.
# Licence
Basically... go nuts!
Officially... It's Apache 2.0. See
[LICENCE.md](https://git.hazaarlabs.com/hazaar/hazaar-thread/master/LICENCE.md)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment