php多任务程序实例解析
作者:shichen2014 时间:2023-11-18 00:22:09
本文以实例简单解析了php多任务程序的实现方法,具体代码如下:
<?php
error_reporting(E_ALL);
set_time_limit(0);
/**
* php多任务程序的实现
* 借助proc_open
* 其实该叫进程(process)
* 能启动多进程,你可以使用你的想象力做你想做的了,以后再写个能用的
* 如果你是在linux上跑php,并且启用pcntl模块后,使用pcntl函数该更好
*
*/
class Thread {
protected $_pref; // process reference
protected static $_instance = null;
protected $_pipes;
private function __construct() {
$this->_pref = 0;
}
public static function getInstance($file) {
if (null == self::$_instance) {
self::$_instance = new self;
}
$descriptor = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "./error-output.txt", "a"),
);
self::$_instance->_pref = proc_open("php -q $file", $descriptor, self::$_instance->_pipes);
return true;
}
public function __destruct() {
proc_close($this->_pref);
$this->_pref = null;
}
}
// 测试代码
$file = __FILE__;
if(empty($argv[1])) {
$t2 = Thread::getInstance("$file 1");
$t3 = Thread::getInstance("$file 2");
$t4 = Thread::getInstance("$file 3");
$t5 = Thread::getInstance("$file 4");
$t5 = Thread::getInstance("$file 5");
$t5 = Thread::getInstance("$file 6");
$t2 = Thread::getInstance("$file 7");
$t3 = Thread::getInstance("$file 8");
$t4 = Thread::getInstance("$file 9");
$t5 = Thread::getInstance("$file 10");
$t5 = Thread::getInstance("$file 11");
$t5 = Thread::getInstance("$file 12");
echo "Main thread done\n";
} else {
$somecontent = "\r\n//~~~~~~~~~~~~-这次请求序号是:" . $argv[1];
sleep(mt_rand(0, 3));
$handle = fopen($file, 'a+');
fwrite($handle, $somecontent);
}
标签:php,多任务,程序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQLServer 连接失败错误故障的分析与排除
2024-01-24 09:09:42
![](https://img.aspxhome.com/file/2023/2/106222_0s.jpg)
Sqlserver 2000/2005/2008 的收缩日志方法和清理日志方法
2012-07-21 14:55:18
Mac OS下PHP环境搭建及PHP操作MySQL常用方法小结
2024-05-08 10:16:31
![](https://img.aspxhome.com/file/2023/4/128084_0s.jpg)
Oracle数据库"记录被另一个用户锁住"解决方法(推荐)
2024-01-19 18:33:25
python多进程 主进程和子进程间共享和不共享全局变量实例
2022-11-05 11:42:56
![](https://img.aspxhome.com/file/2023/1/85261_0s.jpg)
理解JavaScript中的事件 Event
2008-03-19 11:16:00
理解Sql Server中的聚集索引
2024-01-23 11:51:46
![](https://img.aspxhome.com/file/2023/3/91193_0s.png)
关于H1的用法探讨
2008-03-18 12:55:00
如何基于Python实现自动扫雷
2023-12-13 15:51:08
![](https://img.aspxhome.com/file/2023/8/85128_0s.jpg)
Django 配置多站点多域名的实现步骤
2022-05-23 23:53:46
使用Python实现大学座位预约功能
2022-03-14 16:26:53
![](https://img.aspxhome.com/file/2023/4/127594_0s.png)
使SQL用户只能看到自己拥有权限的库(图文教程)
2024-01-27 11:07:27
![](https://img.aspxhome.com/file/2023/6/129296_0s.png)
用js实现键盘方向键翻页功能的代码
2024-04-18 09:36:49
分享Python文本生成二维码实例
2023-12-16 09:42:28
PHP中合并数组的常见方法分享
2023-05-25 12:02:48
python计算方程式根的方法
2023-08-09 09:54:47
Golang排列组合算法问题之全排列实现方法
2023-07-14 14:16:19
详解tf.device()指定tensorflow运行的GPU或CPU设备实现
2021-03-25 19:39:27
![](https://img.aspxhome.com/file/2023/7/110407_0s.jpg)
Pandas处理DataFrame稀疏数据及维度不匹配数据分析详解
2023-08-20 02:44:38
![](https://img.aspxhome.com/file/2023/9/75999_0s.jpg)
设置SQLServer数据库中某些表为只读的多种方法分享
2012-07-11 15:41:05