php 实现多线程,php多线程
php 实现多线程,php多线程
通过php的Socket方式实现php程序的多线程。php本身是不支持多线程的,那么如何在php中实现多线程呢?可以想一下,WEB服务器本身都是支持多线程的。每一个访问者,当访问WEB页面的时候,都将调用新的线程,通过这一点我们可以利用WEB服务器自身的线程来解决PHP不支持多线程的问题。
下面给出通过 fsockopen() 建立socket连接,然后用 用fputs() 发送消息,来实现的PHP多线程类代码:
$fp=fsockopen($_SERVER['HTTP_HOST'],80,&$errno,&$errstr,5);
if(!$fp){
echo "$errstr ($errno)
\n";
}
fputs($fp,"GET $_SERVER[PHP_SELF]?flag=1\r\n");
fclose($fp);
上面这段代码只是一个线程的操作过程。多进行几个这样的操作就是多线程了。目前所谓PHP的多线程程序都是基于这个方式的。
下面给一个完整的线程类代码。
/**
@title:PHP多线程类(Thread)
@version:1.0
@author:axgle
*/
class thread {
var $count;
function thread($count=1) {
$this->count=$count;
}
function _submit() {
for($i=1;$icount;$i++) $this->_thread();
return true;
}
function _thread() {
$fp=fsockopen($_SERVER['HTTP_HOST'],80,&$errno,&$errstr,5);
if(!$fp){
echo "$errstr ($errno)
\n";
}
fputs($fp,"GET $_SERVER[PHP_SELF]?flag=1\r\n");
fclose($fp);
}
function exec($func) {
isset($_GET['flag'])?call_user_func($func):$this->_submit();
}
}
//应用例子:
$th=new thread(10);//10个线程
$th->exec('demo');//执行行自定义的函数
function demo() {
fopen('data/'.microtime(),'w');
}
?>
http://codechina.spaces.live.com/blog/cns!bca6db10a924c24!575.entry

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
