php mysql 多线程_PHP 高级编程之多线程

PHP 高级编程之多线程

目录

1. 多线程环境安装

1.1. PHP 5.5.9

1.2. 安装 pthreads 扩展

2. Thread

3. Worker 与 Stackable

4. 互斥锁

4.1. 多线程与共享内存

5. 线程同步

6. 线程池

6.1. pthreads Pool类

6.2. 线程池的原理

6.3. 动态队列线程池

6.4. 等待线程池中的线程运行完毕

7. 多线程文件安全读写(文件锁)

8. 多线程与数据连接

8.1. Worker 与 PDO

8.2. Pool 与 PDO

8.3. 数据库持久连接

8.4. 涉及数据库更新

9. Thread And ZeroMQ

9.1. 数据库端

9.2. 数据处理端

10. 延伸阅读

1. 多线程环境安装

1.1. PHP 5.5.9

安装PHP 5.5.9

./configure --prefix=/srv/php-5.5.9 \

--with-config-file-path=/srv/php-5.5.9/etc \

--with-config-file-scan-dir=/srv/php-5.5.9/etc/conf.d \

--enable-fpm \

--with-fpm-user=www \

--with-fpm-group=www \

--with-pear \

--with-curl \

--with-gd \

--with-jpeg-dir \

--with-png-dir \

--with-freetype-dir \

--with-zlib-dir \

--with-iconv \

--with-mcrypt \

--with-mhash \

--with-pdo-mysql \

--with-mysql-sock=/var/lib/mysql/mysql.sock \

--with-openssl \

--with-xsl \

--with-recode \

--enable-sockets \

--enable-soap \

--enable-mbstring \

--enable-gd-native-ttf \

--enable-zip \

--enable-xml \

--enable-bcmath \

--enable-calendar \

--enable-shmop \

--enable-dba \

--enable-wddx \

--enable-sysvsem \

--enable-sysvshm \

--enable-sysvmsg \

--enable-opcache \

--enable-pcntl \

--enable-maintainer-zts \

--disable-debug

编译必须启用zts支持否则无法安装 pthreads(--enable-maintainer-zts)

1.2. 安装 pthreads 扩展

# curl -s https://raw.github.com/oscm/shell/master/php/pecl/pthreads.sh | bash

查看pthreads是否已经安装

# php -m | grep pthreads

2. Thread

class HelloWorld extends Thread {

public function __construct($world) {

$this->world = $world;

}

public function run() {

print_r(sprintf("Hello %s\n", $this->world));

}

}

$thread = new HelloWorld("World");

if ($thread->start()) {

printf("Thread #%lu says: %s\n", $thread->getThreadId(), $thread->join());

}

?>

3. Worker 与 Stackable

class SQLQuery extends Stackable {

public function __construct($sql) {

$this->sql = $sql;

}

public function run() {

$dbh = $this->worker->getConnection();

$row = $dbh->query($this->sql);

while($member = $row->fetch(PDO::FETCH_ASSOC)){

print_r($member);

}

}

}

class ExampleWorker extends Worker {

public static $dbh;

public function __construct($name) {

}

/*

* The run method should just prepare the environment for the work that is coming ...

*/

public function run(){

self::$dbh = new PDO('mysql:host=192.168.2.1;dbname=example','www','123456');

}

public function getConnection(){

return self::$dbh;

}

}

$worker = new ExampleWorker("My Worker Thread");

$work=new SQLQuery('select * from members order by id desc limit 5');

$worker->stack($work);

$table1 = new SQLQuery('select * from demousers limit 2');

$worker->stack($table1);

$worker->start();

$worker->shutdown();

?>

4. 互斥锁

什么情况下会用到互斥锁?在你需要控制多个线程同一时刻只能有一个线程工作的情况下可以使用。

下面我们举一个例子,一个简单的计数器程序,说明有无互斥锁情况下的不同。

$counter = 0;

//$handle=fopen("php://memory", "rw");

//$handle=fopen("php://temp", "rw");

$handle=fopen("/tmp/counter.txt", "w");

fwrite($handle, $counter );

fclose($handle);

class CounterThread extends Thread {

public function __construct($mutex = null){

$this->mutex = $mutex;

$this->handle = fopen("/tmp/counter.txt", "w+");

}

public function __destruct(){

fclose($this->handle);

}

public function run() {

if($this->mutex)

$locked=Mutex::lock($this->mutex);

$counter = intval(fgets($this->handle));

$counter++;

rewind($this->handle);

fputs($this->handle, $counter );

printf("Thread #%lu says: %s\n", $this->getThreadId(),$counter);

if($this->mutex)

Mutex::unlock($this->mutex);

}

}

//没有互斥锁

for ($i=0;$i<50;$i++){

$threads[$i] = new CounterThread();

$threads[$i]->start();

}

//加入互斥锁

$mutex = Mutex::create(true);

for ($i=0;$i<50;$i++){

$threads[$i] = new CounterThread($mutex);

$threads[$i]->start();

}

Mutex::unlock($mutex);

for ($i=0;$i<50;$i++){

$threads[$i]->join();

}

Mutex::destroy($mutex);

?>

我们使用文件/tmp/counter.txt保存计数器值,每次打开该文件将数值加一,然后写回文件。当多个线程同时操作一个文件的时候,就会线程运行先后取到的数值不同,写回的数值也不同,最终计数器的数值会混乱。

没有加入锁的结果是计数始终被覆盖,最终结果是2

而加入互斥锁后,只有其中的一个进程完成加一工作并释放锁,其他线程才能得到解锁信号,最终顺利完成计数器累加操作

上面例子也可以通过对文件加锁实现,这里主要讲的是多线程锁,后面会涉及文件锁。

4.1. 多线程与共享内存

在共享内存的例子中,没有使用任何锁,仍然可能正常工作,可能工作内存操作本身具备锁的功能。

$tmp = tempnam(__FILE__, 'PHP');

$key = ftok($tmp, 'a');

$shmid = shm_attach($key);

$counter = 0;

shm_put_var( $shmid, 1, $counter );

class CounterThread extends Thread {

public function __construct($shmid){

$this->shmid = $shmid;

}

public function run() {

$counter = shm_get_var( $this->shmid, 1 );

$counter++;

shm_put_var( $this->shmid, 1, $counter );

printf("Thread #%lu says: %s\n", $this->getThreadId(),$counter);

}

}

for ($i=0;$i<100;$i++){

$threads[] = new CounterThread($shmid);

}

for ($i=0;$i<100;$i++){

$threads[$i]->start();

}

for ($i=0;$i<100;$i++){

$threads[$i]->join();

}

shm_remove( $shmid );

shm_detach( $shmid );

?>

5. 线程同步

有些场景我们不希望 thread->start() 就开始运行程序,而是希望线程等待我们的命令。

$thread->wait();测作用是 thread->start()后线程并不会立即运行,只有收到 $thread->notify(); 发出的信号后才运行

$tmp = tempnam(__FILE__, 'PHP');

$key = ftok($tmp, 'a');

$shmid = shm_attach($key);

$counter = 0;

shm_put_var( $shmid, 1, $counter );

class CounterThread extends Thread {

public function __construct($shmid){

$this->shmid = $shmid;

}

public function run() {

$this->synchronized(function($thread){

$thread->wait();

}, $this);

$counter = shm_get_var( $this->shmid, 1 );

$counter++;

shm_put_var( $this->shmid, 1, $counter );

printf("Thread #%lu says: %s\n", $this->getThreadId(),$counter);

}

}

for ($i=0;$i<100;$i++){

$threads[] = new CounterThread($shmid);

}

for ($i=0;$i<100;$i++){

$threads[$i]->start();

}

for ($i=0;$i<100;$i++){

$threads[$i]->synchronized(function($thread){

$thread->notify();

}, $threads[$i]);

}

for ($i=0;$i<100;$i++){

$threads[$i]->join();

}

shm_remove( $shmid );

shm_detach( $shmid );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值