手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2025年07月的文章

[向东]简单的PHP的任务队列

来自向东:http://www.xiangdong.org/blog/post/1743/

文章太长,不作过多介绍,反正,文章的头部就说明了大概的意思。。。
原文如下:
写了一个简单的队列任务处理。多进程任务,异步任务可能会用到这个(主要是命令行应用)
比如,任务的某个一个环节速度十分不稳定,可能执行几秒,也可能执行几分钟,
我就可以把那个环节包括前面的部分扔进队列,多跑几个进程,同时往队列里面写。
然后后面比较快的环节只跑一个处理任务就OK了。让整体速度达到更好的效果。

write.php: 将任务写入队列

PHP代码
  1. <?php  
  2. /* 
  3. 产生队列 
  4. */  
  5. //用微秒生成队列文件名。因为会有多个队列,所以加了一个identifier来区分各个队列  
  6. function mt($identifier='default')  
  7. {  
  8.         return sprintf("%.6f.%s",strtok(microtime(),' ')+strtok(''),$identifier);  
  9. }  
  10.   
  11. while(1) //实际中尽量不要while(1) 非要while(1)记得任务完成要break  
  12. {  
  13.         if(count(glob('./queue/*.identifier'))>=10) //队列最大长度,不限制的话硬盘可能会受不了哦。  
  14.         {  
  15.                 sleep(1);//记住这里要sleep,否则队列满了cpu占用很高  
  16.                 continue;  
  17.         }  
  18.         $url = 'www.'.time().'.com'//随便举个例子,我用时间戳生成了一个网址  
  19.         echo "$url\r\n";  
  20.         $fp = fopen('./queue/'.mt('identifier'),'w');  
  21.         fwrite($fp,$url);  
  22.         fclose($fp);  
  23.         sleep(1);//这里不需要sleep,我sleep是因为我的任务太简单。  
  24. }  
  25. ?>  

read.php:

PHP代码
  1. <?php  
  2. /* 
  3. 处理队列 
  4. */  
  5.   
  6. while(1) //实际程序最好不要while(1)如果while(1),记得处理完任务要break  
  7. {  
  8.         if($queue = glob('./queue/*.identifier'))  
  9.         {  
  10.                 $q = array_shift($queue);  
  11.                 $url = file_get_contents($q);  
  12.                 echo $url."\r\n";  
  13.                 unlink($q);  
  14.         }  
  15.         sleep(1);//这里要不要sleep或sleep多久自己凭感觉来。  
  16. }  
  17. ?>  

相关的资料:双向队列

baidu和google上没有查到PHP双向队列的资料,搜索到java的双向队列定义如下:双向队列(双端队列)就像是一个队列,但是你可以在任何一端添加或移除元素。
而双端队列是一种数据结构,定义如下:
A deque is a data structure consisting of a list of items, on which the following operations are possible.
* push(D,X) -- insert item X on the rear end of deque D.
* pop(D) -- remove the front item from the deque D and return it.
* inject(D,X) -- insert item X on the front end of deque D.
* eject(D) -- remove the rear item from the deque D and return it.
Write routines to support the deque that take O(1) time per operation.

翻译:双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作:
push(D,X) 将项X 插入到双端队列D的前端
pop(D) 从双端队列D中删除前端项并将其返回
inject(D,X) 将项X插入到双端队列D的尾端
eject(D) 从双端队列D中删除尾端项并将其返回
编写支持双端队伍的例程,每种操作均花费O(1)时间

百度百科:(deque,全名double-ended queue)是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。

转贴一个使用Php数组函数实现该功能的代码:

PHP代码
  1. <?php  
  2. //Input limit double-ende queue  
  3. class DoubleEndedQueue1 {  
  4. var $queue = array();  
  5. function add($var){  
  6.     return array_push($this->queue, $var);  
  7. }  
  8. function frontRemove(){  
  9.     return array_shift($this->queue);  
  10. }  
  11. function rearRemove(){  
  12.     return array_pop($this->queue);  
  13. }  
  14. }  
  15.   
  16. //Output limit double-ende queue  
  17. class DoubleEndedQueue2 {  
  18. var $queue = array();  
  19. function remove(){  
  20.     return array_pop($this->queue);  
  21. }  
  22. function frontAdd($var){  
  23.     return array_unshift($this->queue, $var);  
  24. }  
  25. function rearAdd($var){  
  26.     return array_push($this->queue, $var);  
  27. }  
  28. }  
  29.   
  30. //Test code  
  31. $q = new DoubleEndedQueue1;  
  32. $q->add('aaa');  
  33. $q->add('bbb');  
  34. $q->add('ccc');  
  35. $q->add('ddd');  
  36.   
  37. echo $q->frontRemove();  
  38. echo "<br>";  
  39. echo $q->rearRemove();  
  40. echo "<br>";  
  41. print_r($q->queue);  
  42. ?>  
array_push -- 将一个或多个单元压入数组的末尾(入栈)
array_unshift -- 在数组开头插入一个或多个单元
array_pop -- 将数组最后一个单元弹出(出栈)
array_shift -- 将数组开头的单元移出数组

// 来自 PHP5 in Practice  (U.S.)Elliott III & Jonathan D.Eisenhamer
PHP代码
  1. <?php  
  2. // A library to implement queues in PHP via arrays  
  3. // The Initialize function creates a new queue:  
  4. function &queue_initialize() {  
  5.     // In this case, just return a new array  
  6.     $new = array();  
  7.     return $new;  
  8. }  
  9. // The destroy function will get rid of a queue  
  10. function queue_destroy(&$queue) {  
  11.     // Since PHP is nice to us, we can just use unset  
  12.     unset($queue);  
  13. }  
  14. // The enqueue operation adds a new value unto the back of the queue  
  15. function queue_enqueue(&$queue$value) {  
  16.     // We are just adding a value to the end of the array, so can use the  
  17.     //  [] PHP Shortcut for this.  It's faster than using array_push  
  18.     $queue[] = $value;  
  19. }  
  20. // Dequeue removes the front of the queue and returns it to you  
  21. function queue_dequeue(&$queue) {  
  22.     // Just use array unshift  
  23.     return array_shift($queue);  
  24. }  
  25. // Peek returns a copy of the front of the queue, leaving it in place  
  26. function queue_peek(&$queue) {  
  27.     // Return a copy of the value found in front of queue  
  28.     //  (at the beginning of the array)  
  29.     return $queue[0];  
  30. }  
  31. // Size returns the number of elements in the queue  
  32. function queue_size(&$queue) {  
  33.     // Just using count will give the proper number:  
  34.     return count($queue);  
  35. }  
  36. // Rotate takes the item on the front and sends it to the back of the queue.  
  37. function queue_rotate(&$queue) {  
  38.     // Remove the first item and insert it at the rear.  
  39.     $queue[] = array_shift($queue);  
  40. }  
  41. // Let's use these to create a small queue of data and manipulate it.  
  42. // Start by adding a few words to it:  
  43. $myqueue =& queue_initialize();  
  44. queue_enqueue($myqueue'Opal');  
  45. queue_enqueue($myqueue'Dolphin');  
  46. queue_enqueue($myqueue'Pelican');  
  47. // The queue is: Opal Dolphin Pelican  
  48. // Check the size, it should be 3  
  49. echo '<p>Queue size is: ', queue_size($myqueue), '</p>';  
  50. // Peek at the front of the queue, it should be: Opal  
  51. echo '<p>Front of the queue is: ', queue_peek($myqueue), '</p>';  
  52. // Now rotate the queue, giving us: Dolphin Pelican Opal  
  53. queue_rotate($myqueue);  
  54. // Remove the front element, returning: Dolphin  
  55. echo '<p>Removed the element at the front of the queue: ',  
  56.     queue_dequeue($myqueue), '</p>';  
  57. // Now destroy it, we are done.  
  58. queue_destroy($myqueue);  
  59. ?>  

Tags: 任务队列

猪流感来袭,网友们小心

03年的SARS,05年左右的禽流感,今年爆发的猪流感
每隔几年总要有点新鲜的病毒出来

网友们,注意身体,没事跑跑步健健身啥的
不该去的地方不要去,以防万一啊

当年SARS是隔离。如今老美的病例几乎是中一个倒一个。
坚持啊

Tags: 猪流感, 禽流感, sars

征集PHPOO.COM的未来走向

PHPOO.com目前纯粹是用作UCHOME,原本我的想法也是类似于当成一个多用户博客来运作的。
但事实上,效果并不明显。

所以,我现在在考虑,是否使用supesite之类的CMS,把它打造成一个PHP的资讯、学习站点。
征集一下意见。。。

我现在的博客上的一部分资料转移过去,同时,收集一些比较经典、常用的资料。
再进行细分,不知道是否可行。

Tags: phpoo

几个ZendStudio使用教程

Zendstudio.net,看这个网站的名称就知道,它是专注于zs工具的。
事实上也确实是这样的。站长还提供了一些KEY的生成工具,阿弥陀佛,罪过罪过。。

站长不但对ZS有研究,同时还本着开源的心态,把自己的一些心得整理出来,是flash的哦。大家可以在线观看。。
站长把一些5.5和6.1的使用教程都列出来了。
如果您是使用6.1的话,那就比较方便了。。。直接打开:http://www.zendstudio.net/zend-studio-tutorial/,就可以看到教程。

  1. zend studio 6.1中文视频教程-快速入门
  2. Zend Studio For Eclipse 6.1 视频教程-调试php程序
  3. 让zend studio for eclipse支持xdebug(视频教程)
  4. 在zend studio for eclipse中使用xdebug调试php程序
  5. 在zend studio for eclipse中寻找消失了的zend studio 5.5特色功能(筹备中…)
  6. 使用profile来优化你的php程序性能,提高程序效率(筹备中…)

新站上只有6.1的教程,如果你还在使用ZS 5.5,不用担心,这里也有:

  1. 基础部分:
    1. ZDE的初级安装教程(图)
    2. Zend Studio 5.5.1 界面详细介绍及菜单、工具栏功能详解(图)
    3. Zend Studio 首选项(preferences)功能、设定详解(图)
    4. Zend Studio的特色功能——模板(templates)
    5. zend studio的特色功能——代码片段(code snippets)
    6. Zend Studio快捷键一览表
    7. zend studio 5.5无法打开主界面故障解决方法一例
    8. 修改zend studio 的默认字体 让编辑器看起来更舒服
    9. 用Zend Studio管理数据库—zde的SQL管理器功能介绍

  2. 提高部分:
    1. 服务器调试——Zend Debugger 的安装教程
    2. Zend Studio的远程调试(服务器调试)技术 Zend Debugger(一)
    3. Zend studio的项目管理和服务器调试(视频教程)

-EOF-
写了这么多,也贴了N多链接,就再贴一下zendstudio.net站长的资料吧。。。
大家随便YY去吧

  • 昵称:徐徐
  • 英文名:gently
  • MSN:my-msn
  • 身高:跟姚明没得比
  • 三围:您所在的用户组无权查看该信息
  • 介绍:目前处于IT食物链底层,若在万恶的旧社会,那就是被剥削被压迫的那个阶层。
  • 人生关键词:积极、自信、踏实。

Tags: zendstudio, php, 教程

SSH连接时的乱码解决方法

ssh连接时,发现屏幕上一堆乱码,恐怕这种事情谁都遇到过吧。(我是使用SSH secure shell登录的)
这种情况的发生大多是安装时,语言包选择为中文导致的。一般有以下几种解决方法

1、RH(估计centOS也行)
vim /etc/sysconfig/i18n
内容改为:
LANG="zh_CN.GB18030"
LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"
SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"
SYSFONT="lat0-sun16"

2、UBUNTU
中文版的ubuntu遇到这种问题,可以尝试使用putty,因为putty可以通过修改 font, character set 设置来解决。
设置:
Window -> Appearance -> Font settings 选择宋体或新宋体:
Window -> Translation -> Character set translation on received data 选择 UTF-8:
然后基本就没有问题了,

或者尝试使用secureCRT登录,这款软件也能够自动识别。

再或者尝试把语言换为英文?
修改Ubuntu的命令行语言环境的2个步骤:

1、修改/etc/default/locale
如不存在则新建一个
如下:
LANG='en_US' #中文可以用zh_CN
LANGUAGE='en_US:en' #中文可以用zh_CN:zh

2、reboot即可
locale命令可以列出当前系统所用的所有语言设置

即使你搜索google,基本上也只有这几种解决方案了。呵呵,全部列出来,希望有用

Tags: linux, ssh, 乱码