简单的PHP的任务队列
文章太长,不作过多介绍,反正,文章的头部就说明了大概的意思。。。
原文如下:
写了一个简单的队列任务处理。多进程任务,异步任务可能会用到这个(主要是命令行应用)
比如,任务的某个一个环节速度十分不稳定,可能执行几秒,也可能执行几分钟,
我就可以把那个环节包括前面的部分扔进队列,多跑几个进程,同时往队列里面写。
然后后面比较快的环节只跑一个处理任务就OK了。让整体速度达到更好的效果。
write.php: 将任务写入队列
- <?php
- /*
- 产生队列
- */
- //用微秒生成队列文件名。因为会有多个队列,所以加了一个identifier来区分各个队列
- function mt($identifier='default')
- {
- return sprintf("%.6f.%s",strtok(microtime(),' ')+strtok(''),$identifier);
- }
- while(1) //实际中尽量不要while(1) 非要while(1)记得任务完成要break
- {
- if(count(glob('./queue/*.identifier'))>=10) //队列最大长度,不限制的话硬盘可能会受不了哦。
- {
- sleep(1);//记住这里要sleep,否则队列满了cpu占用很高
- continue;
- }
- $url = 'www.'.time().'.com'; //随便举个例子,我用时间戳生成了一个网址
- echo "$url\r\n";
- $fp = fopen('./queue/'.mt('identifier'),'w');
- fwrite($fp,$url);
- fclose($fp);
- sleep(1);//这里不需要sleep,我sleep是因为我的任务太简单。
- }
- ?>
read.php:
- <?php
- /*
- 处理队列
- */
- while(1) //实际程序最好不要while(1)如果while(1),记得处理完任务要break
- {
- if($queue = glob('./queue/*.identifier'))
- {
- $q = array_shift($queue);
- $url = file_get_contents($q);
- echo $url."\r\n";
- unlink($q);
- }
- sleep(1);//这里要不要sleep或sleep多久自己凭感觉来。
- }
- ?>
相关的资料:双向队列
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
- //Input limit double-ende queue
- class DoubleEndedQueue1 {
- var $queue = array();
- function add($var){
- return array_push($this->queue, $var);
- }
- function frontRemove(){
- return array_shift($this->queue);
- }
- function rearRemove(){
- return array_pop($this->queue);
- }
- }
- //Output limit double-ende queue
- class DoubleEndedQueue2 {
- var $queue = array();
- function remove(){
- return array_pop($this->queue);
- }
- function frontAdd($var){
- return array_unshift($this->queue, $var);
- }
- function rearAdd($var){
- return array_push($this->queue, $var);
- }
- }
- //Test code
- $q = new DoubleEndedQueue1;
- $q->add('aaa');
- $q->add('bbb');
- $q->add('ccc');
- $q->add('ddd');
- echo $q->frontRemove();
- echo "<br>";
- echo $q->rearRemove();
- echo "<br>";
- print_r($q->queue);
- ?>
array_push -- 将一个或多个单元压入数组的末尾(入栈)
array_unshift -- 在数组开头插入一个或多个单元
array_pop -- 将数组最后一个单元弹出(出栈)
array_shift -- 将数组开头的单元移出数组
// 来自 PHP5 in Practice (U.S.)Elliott III & Jonathan D.Eisenhamer
- <?php
- // A library to implement queues in PHP via arrays
- // The Initialize function creates a new queue:
- function &queue_initialize() {
- // In this case, just return a new array
- $new = array();
- return $new;
- }
- // The destroy function will get rid of a queue
- function queue_destroy(&$queue) {
- // Since PHP is nice to us, we can just use unset
- unset($queue);
- }
- // The enqueue operation adds a new value unto the back of the queue
- function queue_enqueue(&$queue, $value) {
- // We are just adding a value to the end of the array, so can use the
- // [] PHP Shortcut for this. It's faster than using array_push
- $queue[] = $value;
- }
- // Dequeue removes the front of the queue and returns it to you
- function queue_dequeue(&$queue) {
- // Just use array unshift
- return array_shift($queue);
- }
- // Peek returns a copy of the front of the queue, leaving it in place
- function queue_peek(&$queue) {
- // Return a copy of the value found in front of queue
- // (at the beginning of the array)
- return $queue[0];
- }
- // Size returns the number of elements in the queue
- function queue_size(&$queue) {
- // Just using count will give the proper number:
- return count($queue);
- }
- // Rotate takes the item on the front and sends it to the back of the queue.
- function queue_rotate(&$queue) {
- // Remove the first item and insert it at the rear.
- $queue[] = array_shift($queue);
- }
- // Let's use these to create a small queue of data and manipulate it.
- // Start by adding a few words to it:
- $myqueue =& queue_initialize();
- queue_enqueue($myqueue, 'Opal');
- queue_enqueue($myqueue, 'Dolphin');
- queue_enqueue($myqueue, 'Pelican');
- // The queue is: Opal Dolphin Pelican
- // Check the size, it should be 3
- echo '<p>Queue size is: ', queue_size($myqueue), '</p>';
- // Peek at the front of the queue, it should be: Opal
- echo '<p>Front of the queue is: ', queue_peek($myqueue), '</p>';
- // Now rotate the queue, giving us: Dolphin Pelican Opal
- queue_rotate($myqueue);
- // Remove the front element, returning: Dolphin
- echo '<p>Removed the element at the front of the queue: ',
- queue_dequeue($myqueue), '</p>';
- // Now destroy it, we are done.
- queue_destroy($myqueue);
- ?>
简单的PHP的任务队列的更多相关文章
- 简单易用的任务队列-beanstalkd
概述 beanstalkd 是一个简单快速的分布式工作队列系统,协议基于 ASCII 编码运行在 TCP 上.其最初设计的目的是通过后台异步执行耗时任务的方式降低高容量 Web 应用的页面延时.其具有 ...
- 探究ElasticSearch中的线程池实现
探究ElasticSearch中的线程池实现 ElasticSearch里面各种操作都是基于线程池+回调实现的,所以这篇文章记录一下java.util.concurrent涉及线程池实现和Elasti ...
- es6 Promise 事件机制分析
最近在学习es6的Promise,其中涉及到了Promsie的事件执行机制,因此总结了关于Promise的执行机制,若有错误,欢迎纠错和讨论. 在阮一峰老师的书中<es6 标准入门>对Pr ...
- 纯粹极简的react状态管理组件unstated
简介 unstated是一个极简的状态管理组件 看它的简介:State so simple, it goes without saying 对比 对比redux: 更加灵活(相对的缺点是缺少规则,需要 ...
- Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池
前言:由于最近在做SDK的功能,需要设计线程池.看了很多资料不知道从何开始着手,突然发现了AsyncTask有对线程池的封装,so,就拿它开刀,本文将从AsyncTask的基本用法,到简单的封装,再到 ...
- Python—异步任务队列Celery简单使用
一.Celery简介 Celery是一个简单,灵活,可靠的分布式系统,用于处理大量消息,同时为操作提供维护此类系统所需的工具.它是一个任务队列,专注于实时处理,同时还支持任务调度. 中间人boker: ...
- 在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部分 ...
- 用redis做简单的任务队列(二)
是用redis做任务队列时,要思考: 用什么数据类型来做任务队列 怎样才能防止重复爬取 上一篇文章已经决定使用list来做任务队列,但是去重问题没有得到解决.这里可以用set来解决思考二的问题,就是防 ...
- 用redis做简单的任务队列(一)
队列本身其实是个有序的列表,而Redis是支持list的,我们可以查看Redis的官方文档 http://redis.io/commands#list,其中我们可以对这个队列的两端分别进行操作,所以其 ...
随机推荐
- Webstorm 的 Tab 键怎样调整缩进值? 调节成缩进成2个空格或者4个空格
原文:https://blog.csdn.net/niexia_/article/details/78280569 需求:因为用了eslint,对代码格式很严格.统一用空格.而用tab方式会省事很多. ...
- tomcat安装配置常见问题详解
历经波折,终于把tomcat装好了.记录下过程供自己和后来的初学者参考吧! 本文先后介绍了tomcat的下载安装方法.安装和启动不成功的常见原因 以及启动tomcat后如何配置上下文. 一.下载安装 ...
- vue 学前班001(基础概念)
1 学习目标 通过这一节,你会学会: 1.目前前端技术使用的趋势 2.什么是MVVM 3.Vue.js的两大核心 4.Vue.js的适用场景 诞生背景 近几年来,得益于手机设备的普及和性能的提升, ...
- Python 去除列表中重复的元素
Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...
- apt小问题
安装软件遇到情况,一直等待: root@test-xxx:/opt# apt-get install vsftpdReading package lists... DoneBuilding depen ...
- 【wireshark】协议解析
1. 普通解析 Wireshark启动时,所有解析器进行初始化和注册.要注册的信息包括协议名称.各个字段的信息.过滤用的关键字.要关联的下层协议与端口(handoff)等.在解析过程,每个解析器负责解 ...
- 记一次使用SecureCRT连接局域网巨慢的问题
环境:Win7 32bit + SecureCRT 6.5 中文 使用工作机上的SecureCRT登录公司内网的跳板机,发现很慢,每次都得等待好几分钟才弹出输出私匙密码的框.咨询了一下其他同事,发现他 ...
- WEB-INFO 目录
WEB-INF下面的内容都是只能由服务器级别才能访问,客户端并不能访问. 转发就是服务器级别,浏览器的地址不会变,因为,客户端发送一个请求,服务器受理之后,发现要请求内容还要再去别的请求,那么转发就是 ...
- 【Azure】Publish Error of "%(TargetOSFamily.Identity)" that evaluates to "" instead of a number
在向Azure部署程序的时候,出现如下错误: A numeric comparison was attempted on "%(TargetOSFamily.Identity)" ...
- 剑指offer二十五之复杂链表的复制
一.题目 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...