PHP多进程问题 与 多继承问题
一、多进程问题:
概述:经常在项目架构中会考虑到该系统的负载程度、并发程度,需不需要多进程或者多服务器负载来均衡,其中就包括多线程处理这个处理想法。
引用某PHP大牛的一段话:
我之前的文章中说过,大多数网站的性能瓶颈不在PHP服务器上,因为它可以简单地通过横向增加服务器或CPU核数来轻松应对(对于各种云主机,增加VPS或CPU核数就更方便了,直接以备份镜像增加VPS,连操作系统、环境都不用安装配置),而是在于MySQL数据库。如果用 MySQL 数据库,一条联合查询的SQL,也许就可以处理完业务逻辑,但是,遇到大量并发请求,就歇菜了。如果用 NoSQL 数据库,也许需要十次查询,才能处理完同样地业务逻辑,但每次查询都比 MySQL 要快,十次循环NoSQL查询也许比一次MySQL联合查询更快,应对几万次/秒的查询完全没问题。如果加上PHP多线程,通过十个线程同时查询NoSQL,返回结果汇总输出,速度就要更快了。我们实际的APP产品中,调用一个通过用户喜好实时推荐商品的PHP接口,PHP需要对BigSea NoSQL数据库发起500~1000次查询,来实时算出用户的个性喜好商品数据,PHP多线程的作用非常明显。
PHP 的多线程指的应该是 pecl 中的 pthreads。
这个东西很少有人会用到,不建议大家用,因为通常情况需要用到 pthreads 的时候通常是这个工作不适合 PHP 来做;而且pthreads会在创建和变动进程的时候消耗一定的服务器资源。
如果说 PHP 和多线程有什么关系,那就是 PHP 不同于其他的一些后端语言,通常 PHP 的运行环境是由 PHP-FPM管理的若干个独立的 PHP 进程组成的。
因此 PHP 天然地可以非常方便地横向扩充:增加 PHP-FPM 的进程数,甚至把 PHP-FPM分散到多台服务器上。
本站pthreads的早期版本使用:
二、多继承问题
用到多继承的情况:
1.一般在业务扩展又不想影响原来类的使用;
2.不同业务需求,但是又有一定的关联性,所以在基类的基础上实现多继承。
多继承的设计方式:
1.使用观察者模式:
/** * 父类一 */ class Parent1 { function method1() {} function method2() {} } /** * 父类二 */ class Parent2 { function method3() {} function method4() {} } /** * 子类 */ class Child { protected $_parents = array(); public function Child(array $parents=array()) { $this->_parents = $parents; } public function __call($method, $args) { // 从“父类"中查找方法 foreach ($this->_parents as $p) { if (is_callable(array($p, $method))) { return call_user_func_array(array($p, $method), $args); } } // 恢复默认的行为,会引发一个方法不存在的致命错误 return call_user_func_array(array($this, $method), $args); } } $obj = new Child(array(new Parent1(), new Parent2())); $obj->method1(); $obj->method3();
2.使用间接接口链条继承方式:
interface testA{ function echostr(); } interface testB extends testA{ function dancing($name); } class testC implements testB{ function echostr(){ echo "接口继承,要实现所有相关抽象方法!"; echo "<br>"; } function dancing($name){ echo $name."正在跳舞!"; } } $demo=new testC(); $demo->echostr(); $demo->dancing("模特");
3.使用PHP5.4以后支持的Trait片段代码多继承方式(Trait即聚合方式):
Trait看上去既像类又像接口,其实都不是,Trait可以看做类的部分实现,可以混入一个或多个现有的PHP类中,其作用有两个:表明类可以做什么;提供模块化实现。
Trait是一种代码复用技术,为PHP的单继承限制提供了一套灵活的代码复用机制。
trait ezcReflectionReturnInfo { function getReturnType() { /*1*/ } function getReturnDescription() { /*2*/ } } class ezcReflectionMethod extends ReflectionMethod { use ezcReflectionReturnInfo; /* ... */ } class ezcReflectionFunction extends ReflectionFunction { use ezcReflectionReturnInfo; /* ... */ }
Tags In
Jocsonme
Related Posts
分类目录
- Apache (6)
- APP-Android (1)
- APP-IOS (3)
- BI (2)
- Qlikview (2)
- BigData (2)
- Domain-Space (1)
- Git (4)
- Google (1)
- History – Articles (2)
- HTML+CSS (13)
- IDE-CodeEditor (1)
- NetBeans (1)
- Javascript (18)
- Linux (24)
- Load-Balancing (3)
- Memcached (1)
- MySQL (9)
- PHP (28)
- Laravel (1)
- Swoole (1)
- Symfony2.x – 3.x (2)
- ThinkPHP5 (1)
- Thirdpart – develop (6)
- ZendFramework2.x (1)
- Products-Design (1)
- Redis (1)
- SEM (2)
- SEO (2)
- Server-Architecture (5)
- Vagrant (1)
- Windows (7)
- BAT (1)
- Workplace (1)
一 | 二 | 三 | 四 | 五 | 六 | 日 |
---|---|---|---|---|---|---|
« 11月 | ||||||
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |