rogerwaters / react-thread-pool
基于pcntl_fork()和reactphp的PHP多进程库
dev-master
2016-06-09 12:38 UTC
Requires
- php: >=5.4.0
- evenement/evenement: dev-master
- react/event-loop: v0.4.*
Suggests
- ext-event: ~1.0
- ext-libevent: >=0.1.0
This package is not auto-updated.
Last update: 2024-09-14 18:20:39 UTC
README
这是一个基于pcntl_fork()的PHP多进程库。
状态:alpha
测试:无
特性
- 从单个进程运行并行进程
- 无需考虑序列化即可将整个环境提供给线程
- 控制线程(启动、停止、终止)+ 自定义消息
- 在单个ThreadPool上运行大量线程(注意你的处理器)
- 设置线程以观察外部资源
需求
- Linux/Unix平台
- PHP >= 5.4(也适用于PHP 7)
- 自定义函数(pcntl_fork、pcntl_waitpid、posix_getpid)
- reactphp/event-loop
- igorw/evenement
- [可选] PHP 扩展 libevent(pect/libevent-0.1.0)
- [可选] PHP 扩展 Event https://php.ac.cn/manual/en/book.event.php
基本用法
创建一个EventLoop
$loop = ForkableFactory::create();
*循环与reactphp/event-loop相同,因此您也可以将其用于您的服务器
创建一个默认线程以在父进程外部执行繁重工作
use RogerWaters\ReactThreads\EventLoop\ForkableLoopInterface; use RogerWaters\ReactThreads\ThreadBase; class ExampleThread extends ThreadBase { protected function InitializeExternal(ForkableLoopInterface $loop) { //Do your external logic here //you can also use $loop functions //Use $this->kill(); to complete execution from child $this->kill(); } }
全部一起:```php //创建线程 $thread = new ExampleThread($loop); //启动线程并执行外部逻辑 $thread->start();
//等待线程完成 $thread->on('stopped',function() use ($loop) { //线程已完成 //停止父进程 $loop->stop(); });
//您可以在这里执行任何其他操作,而不会影响线程
//运行循环以等待完成 $loop->run();
For communicable threads see example_3.php and example_4.php
## More examples
See /examples folder
## TODO:
- Documentation
- Tests
- More examples
- https://github.com/reactphp/event-loop/issues/41
- Stability / Error handling
If you have any issues or feature request, feel free to create a ticket