Skip to content

Commit d68f5d0

Browse files
author
Justin Jack
committed
Added LICENSE, examples, README.md
1 parent d6e54ec commit d68f5d0

File tree

11 files changed

+513
-58
lines changed

11 files changed

+513
-58
lines changed
File renamed without changes.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# PHPThreads - Powerful easy-to-use parallel processing library for PHP
2+
## By Justin Jack
3+
4+
If you're like me, you **love** PHP! You love the flexibility and power of the language - the freedom to write sloppy code (I don't advocate it, but I'll defend to the death the right to write sloppy code), or to create indisputably beautiful works of logical art! Now, for all my C (and C++, I guess) programmers, you have a library to not only truly show off the power of PHP, but to ***supercharge your next project!***
5+
6+
This library allow you to very efficiently parallel process in a way the very, very closely resembles POSIX multithreading. You can share ***global variables*** between all PHPThreads, pass values/objects/arrays to your PHPTHread at creation, share values/objects/arrays easily between running PHPThreads, and receive PHPThread exit values/objects/arrays when each PHPThread terminates via the `phpthread_join()` family of functions.
7+
8+
I designed this library because I wanted to write a high-performance WebSocket server to easily handle thousands of simultaneous connections through which large amounts of data could be transfered quickly (think images or video). Yes, fundamentally, it's a `fork()`ing server, but the PHPThread layer on top makes it *super* easy to deploy while providing a mechanism for easy (and thread-like) IPC. I could have used C and gotten better performance, but for me, this seemed more portable and easy to deploy as servers are spun up, so now here it is.
9+
10+
For you more technical folks, this library isn't temporally impeded by the need to use "ticks." That's part of what makes it so fast. The library uses signals (e.g. SIGUSR1 and SIGUSR2) internally to interrupt the execution and switch context very fluidly. Yes, we're hogging SIGUSR1 and 2, so if your project is using those signals, consider using `phpthread_send_message()` to a designated message handler function (see: examples/messages1.php and examples/messages2.php). It should completely suit your purpose.
11+
12+
There are examples you can run in the "examples" directory. Feel free to place a shebang for easy execution, or run them by prefixing them with `php <filename>`.
13+
14+
### Dependencies
15+
PHPThreads requires the following PHP extensions:
16+
- posix
17+
- sockets
18+
- pcntl
19+
20+
## Quick and dirty example!
21+
`
22+
<?php
23+
require_once '../lib/phpthread.php'; /* Use correct path! */
24+
25+
function thread_proc( $thread,
26+
$param)
27+
{
28+
echo "\n\t[PHPTHREAD] - I've started and have a param: \"" . print_r($param, true) . "\"!\n";
29+
for ($i = 0; $i < 15; $i++) {
30+
usleep(1000000);
31+
echo "\t[PHPTHREAD] - Tick!\n";
32+
}
33+
$myobj = (object)array(
34+
'seconds_alive'=>$i,
35+
'lucky_number'=>rand(0, 10000)
36+
);
37+
38+
echo "\n\t[PHPTHREAD] - I'm returning this:\n";
39+
print_r($myobj);
40+
echo "\n";
41+
echo "<--------------------------------------->\n\n";
42+
return $myobj;
43+
}
44+
45+
46+
$id = phpthread_create($phpthread, array(), "thread_proc", null, array("this awesome string!"));
47+
48+
for ($i = 0; $i < 5; $i++) {
49+
echo "\n[MAIN] - Doing my thing...\n";
50+
usleep(1000000);
51+
}
52+
53+
echo "\n[MAIN] - Okay, I'm tired. Waiting on child to quit.\n";
54+
phpthread_join($id, $retval);
55+
echo "\n[MAIN] - Child is done. It returned:\n";
56+
print_r($retval);`

examples/client.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)