文章太长,不作过多介绍,反正,文章的头部就说明了大概的意思。。。
原文如下:
写了一个简单的队列任务处理。多进程任务,异步任务可能会用到这个(主要是命令行应用)
比如,任务的某个一个环节速度十分不稳定,可能执行几秒,也可能执行几分钟,
我就可以把那个环节包括前面的部分扔进队列,多跑几个进程,同时往队列里面写。
然后后面比较快的环节只跑一个处理任务就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. while(1) //实际中尽量不要while(1) 非要while(1)记得任务完成要break
  11. {
  12. if(count(glob('./queue/*.identifier'))>=10) //队列最大长度,不限制的话硬盘可能会受不了哦。
  13. {
  14. sleep(1);//记住这里要sleep,否则队列满了cpu占用很高
  15. continue;
  16. }
  17. $url = 'www.'.time().'.com'; //随便举个例子,我用时间戳生成了一个网址
  18. echo "$url\r\n";
  19. $fp = fopen('./queue/'.mt('identifier'),'w');
  20. fwrite($fp,$url);
  21. fclose($fp);
  22. sleep(1);//这里不需要sleep,我sleep是因为我的任务太简单。
  23. }
  24. ?>

read.php:

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

相关的资料:双向队列

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. //Output limit double-ende queue
  16. class DoubleEndedQueue2 {
  17. var $queue = array();
  18. function remove(){
  19. return array_pop($this->queue);
  20. }
  21. function frontAdd($var){
  22. return array_unshift($this->queue, $var);
  23. }
  24. function rearAdd($var){
  25. return array_push($this->queue, $var);
  26. }
  27. }
  28. //Test code
  29. $q = new DoubleEndedQueue1;
  30. $q->add('aaa');
  31. $q->add('bbb');
  32. $q->add('ccc');
  33. $q->add('ddd');
  34. echo $q->frontRemove();
  35. echo "<br>";
  36. echo $q->rearRemove();
  37. echo "<br>";
  38. print_r($q->queue);
  39. ?>

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. ?>

简单的PHP的任务队列的更多相关文章

  1. 简单易用的任务队列-beanstalkd

    概述 beanstalkd 是一个简单快速的分布式工作队列系统,协议基于 ASCII 编码运行在 TCP 上.其最初设计的目的是通过后台异步执行耗时任务的方式降低高容量 Web 应用的页面延时.其具有 ...

  2. 探究ElasticSearch中的线程池实现

    探究ElasticSearch中的线程池实现 ElasticSearch里面各种操作都是基于线程池+回调实现的,所以这篇文章记录一下java.util.concurrent涉及线程池实现和Elasti ...

  3. es6 Promise 事件机制分析

    最近在学习es6的Promise,其中涉及到了Promsie的事件执行机制,因此总结了关于Promise的执行机制,若有错误,欢迎纠错和讨论. 在阮一峰老师的书中<es6 标准入门>对Pr ...

  4. 纯粹极简的react状态管理组件unstated

    简介 unstated是一个极简的状态管理组件 看它的简介:State so simple, it goes without saying 对比 对比redux: 更加灵活(相对的缺点是缺少规则,需要 ...

  5. Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池

    前言:由于最近在做SDK的功能,需要设计线程池.看了很多资料不知道从何开始着手,突然发现了AsyncTask有对线程池的封装,so,就拿它开刀,本文将从AsyncTask的基本用法,到简单的封装,再到 ...

  6. Python—异步任务队列Celery简单使用

    一.Celery简介 Celery是一个简单,灵活,可靠的分布式系统,用于处理大量消息,同时为操作提供维护此类系统所需的工具.它是一个任务队列,专注于实时处理,同时还支持任务调度. 中间人boker: ...

  7. 在windows环境利用celery实现简单的任务队列

    测试使用环境: 1.Python==3.6.1 2.MongoDB==3.6.2 3.celery==4.1.1 4.eventlet==0.23.0 Celery分为3个部分 (1)worker部分 ...

  8. 用redis做简单的任务队列(二)

    是用redis做任务队列时,要思考: 用什么数据类型来做任务队列 怎样才能防止重复爬取 上一篇文章已经决定使用list来做任务队列,但是去重问题没有得到解决.这里可以用set来解决思考二的问题,就是防 ...

  9. 用redis做简单的任务队列(一)

    队列本身其实是个有序的列表,而Redis是支持list的,我们可以查看Redis的官方文档 http://redis.io/commands#list,其中我们可以对这个队列的两端分别进行操作,所以其 ...

随机推荐

  1. elasticsearch中 refresh 和flush区别

    elasticsearch中有两个比较重要的操作:refresh 和 flush refresh操作 当我们向ES发送请求的时候,我们发现es貌似可以在我们发请求的同时进行搜索.而这个实时建索引并可以 ...

  2. ASP.NET MVC Forms验证机制

    ASP.NET MVC 3 使用Forms身份验证 身份验证流程 一.用户登录 1.验证表单:ModelState.IsValid 2.验证用户名和密码:通过查询数据库验证 3.如果用户名和密码正确, ...

  3. Servlet中相对路径与绝对路径

    相对路径与绝对路径: 相对路径:相对路径指的是相对于当前文件所在目录的路径! http://localhost:8080/servlet01/ http://localhost:8080/servle ...

  4. Unity色子的投掷与点数的获得(详解)

    前几天需要一个色子的投掷并且获得朝上点数的Unity脚本,在网上找了很多,都是一个模子刻出来的. 对于2018版的我来说,网上找的都是很早就弃用了的老版本. 好不容易能运行了,结果并不理想,于是又突发 ...

  5. 【PKUSC2018】【loj6433】最大前缀和 状压dp

    这题吼啊... 然而还是想了$2h$,写了$1h$. 我们发现一个性质:若一个序列$p$能作为前缀和,那么在序列$p$中,包含序列$p$最后一个数的所有子序列必然都是非负的. 那么,我们 令$f[i] ...

  6. IOC容器02

    获取xml文件路径,使用IO读取xml文件,使用Document对象解析xml文件,将xml中的bean的信息注册到BeanDefinition类,将BeanDefinition保存到一个map中:该 ...

  7. 实践详细篇-Windows下使用Caffe训练自己的Caffemodel数据集并进行图像分类

    三:使用Caffe训练Caffemodel并进行图像分类 上一篇记录的是如何使用别人训练好的MNIST数据做训练测试.上手操作一边后大致了解了配置文件属性.这一篇记录如何使用自己准备的图片素材做图像分 ...

  8. Vue.js系列之四计算属性和观察者

    一.计算属性 1.模版内的表达式非常便利,但是设计它们的初衷是用于简单计算的.在模版中放入太多的逻辑运算会让模版过重且难以维护,例如如下代码: <div id="example&quo ...

  9. 【树】Lowest Common Ancestor of a Binary Tree(递归)

    题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...

  10. 页面滚动插件 better-scroll 的用法

    better-scroll 是一个页面滚动插件,用它可以很方便的实现下拉刷新,锚点滚动等功能. 实现原理:父容器固定高度,并设置 overflow:hidden,子元素超出父元素高度后将被隐藏,超出部 ...