https://github.com/polonskiy/phproutine
PHProutine is goroutines emulation in PHP
https://github.com/polonskiy/phproutine
async goroutine php
Last synced: 29 days ago
JSON representation
PHProutine is goroutines emulation in PHP
- Host: GitHub
- URL: https://github.com/polonskiy/phproutine
- Owner: polonskiy
- License: mit
- Created: 2014-12-04T16:24:51.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-12-26T11:50:42.000Z (over 11 years ago)
- Last Synced: 2025-10-29T20:45:33.417Z (5 months ago)
- Topics: async, goroutine, php
- Language: PHP
- Homepage:
- Size: 160 KB
- Stars: 60
- Watchers: 5
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# PHProutine
PHProutine is a gouroutines emulation in PHP.
Inspired by Golang and https://gist.github.com/elimisteve/4442820
# Examples
## Goroutines
```go
// Steve Phillips / elimisteve
// 2013.01.03
package main
import "fmt"
// intDoubler doubles the given int, then sends it through the given channel
func intDoubler(ch chan int, n int) {
ch <- n*2
}
func main() {
// Make channel of ints
ch := make(chan int)
answer := make(chan string)
// Spawn 3 goroutines (basically threads) to process data in background
go intDoubler(ch, 10)
go intDoubler(ch, 20)
go func(a, b int) { ch <- a+b }(30, 40) // Take 2 ints, write sum to `ch`
// Create anonymous function on the fly, launch as goroutine!
go func() {
// Save the 3 values passed through the channel as x, y, and z
x, y, z := <-ch, <-ch, <-ch
// Calculate answer, write to `answer` channel
answer <- fmt.Sprintf("%d + %d + %d = %d", x, y, z, x+y+z)
}()
// Print answer resulting from channel read
fmt.Printf("%s\n", <-answer)
}
```
## PHProutines
```php
write($n * 2);
}
$runner = new Runner;
// Make channels
$ch = new Channel;
$answer = new Channel;
// Spawn 3 PHProutines (basically PROCESSES) to process data in background
$runner->go('intDoubler', $ch, 10);
$runner->go('intDoubler', $ch, 20);
$runner->go(function($a, $b) use ($ch) { $ch->write($a + $b); }, 30, 40);
// Create anonymous function on the fly, launch as PHProutine!
$runner->go(function() use ($ch, $answer) {
// Save the 3 values passed through the channel as x, y, and z
list($x, $y, $z) = [$ch->read(), $ch->read(), $ch->read()];
// Calculate answer, write to `answer` channel
$answer->write(sprintf('%d + %d + %d = %d', $x, $y, $z, $x + $y + $z));
});
// Print answer resulting from channel read
printf("%s\n", $answer->read());
```
## Echo server
```php
go(function() use ($client) {
stream_copy_to_stream($client, $client);
});
fclose($client);
}
```
## SleepSort
```php
go(function($v) use ($channel) {
sleep($v);
$channel->write($v);
}, $val);
}
foreach ($argv as $val) {
echo $channel->read(), "\n";
}
```