当前位置: 首页 > news >正文

肇庆高要建设局网站中小学图书馆网站建设

肇庆高要建设局网站,中小学图书馆网站建设,做网站需要懂什么,网页模板免费下载无意间看到这个仓库讲php关于 BeanStalkd 的扩展#xff0c;然后就去了解了一下beanstalkd#xff0c;用它可以用来做队列服务。 话不多说#xff0c;安装一下试试。 首先 sudo apt search beanstalk 搜索一下发现 Sorting... Done Full Text Search... Done awscli/focal…无意间看到这个仓库讲php关于 BeanStalkd 的扩展然后就去了解了一下beanstalkd用它可以用来做队列服务。 话不多说安装一下试试。 首先 sudo apt search beanstalk 搜索一下发现 Sorting... Done Full Text Search... Done awscli/focal-updates,focal-updates 1.18.69-1ubuntu0.20.04.1 all   Universal Command Line Environment for AWS beanstalkd/focal,now 1.11-1 amd64 [installed]   simple, in-memory, workqueue service python-celery-common/focal,focal 4.2.1-5ubuntu1 all   async task/job queue based on message passing (common files) python-celery-doc/focal,focal 4.2.1-5ubuntu1 all   async task/job queue based on message passing (Documentation) python3-celery/focal,focal 4.2.1-5ubuntu1 all   async task/job queue based on message passing (Python3 version) ruby-beaneater/focal,focal 1.0.0-1 all   simple beanstalkd client for Ruby   好了找到这个beanstalkd了 然后安装 sudo apt-get install beanstalkd  Reading package lists... Done Building dependency tree        Reading state information... Done Suggested packages:   doc-base The following NEW packages will be installed:   beanstalkd 0 upgraded, 1 newly installed, 0 to remove and 90 not upgraded. Need to get 43.9 kB of archives. After this operation, 125 kB of additional disk space will be used. Get:1 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 beanstalkd amd64 1.11-1 [43.9 kB] Get:1 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 beanstalkd amd64 1.11-1 [43.9 kB] Fetched 31.3 kB in 4s (7,047 B/s)      Selecting previously unselected package beanstalkd. (Reading database ... 256731 files and directories currently installed.) Preparing to unpack .../beanstalkd_1.11-1_amd64.deb ... Unpacking beanstalkd (1.11-1) ... Setting up beanstalkd (1.11-1) ... Created symlink /etc/systemd/system/multi-user.target.wants/beanstalkd.service → /lib/systemd/system/beanstalkd.service. beanstalkd.socket is a disabled or a static unit, not starting it. Processing triggers for man-db (2.9.1-1) ... Processing triggers for systemd (245.4-4ubuntu3.11) ...   安装成功这个东西会监听 11300 端口 然后使用这个工具来看看 监控工具 wget https://github.com/src-d/beanstool/releases/download/v0.2.0/beanstool_v0.2.0_linux_amd64.tar.gz 获取文件后解压 tar -xvzf beanstool_v0.2.0_linux_amd64.tar.gz 然后拷贝到 /usr/local/bin/ sudo cp beanstool_v0.2.0_linux_amd64/beanstool /usr/local/bin/ 这样就直接用 beanstool 了 查看当前状态 beanstool stats 结果 ------------------------------------------------------------------------------- | Name    | Buried   | Delayed  | Ready    | Reserved | Urgent   | Waiting  | Total    | ------------------------------------------------------------------------------- | default | 0        | 0        | 0        | 0        | 0        | 0        | 0        | ------------------------------------------------------------------------------- 然后使用composer的vendor包 composer require pda/pheanstalk 安装完毕后创建一个input.php文件做生产者 ?php require __DIR__ . /vendor/autoload.php; use Pheanstalk\Pheanstalk; $pheanstalk Pheanstalk::create(127.0.0.1); // Queue a Job $pheanstalk   -useTube(testtube)   -put(job payload goes here\n); $pheanstalk     -useTube(testtube)     -put(         json_encode([test data]),  // encode data in payload         Pheanstalk::DEFAULT_PRIORITY,     // default priority         30, // delay by 30s         60  // beanstalk will retry job after 60s      ); 再创建一个output.php文件做消费者 ?php require __DIR__ . /vendor/autoload.php; use Pheanstalk\Pheanstalk; $pheanstalk Pheanstalk::create(127.0.0.1); // we want jobs from testtube only. $pheanstalk-watch(testtube); // this hangs until a Job is produced. $job $pheanstalk-reserve(); try {     $jobPayload $job-getData();     // do work.     sleep(2);     // If its going to take a long time, periodically     // tell beanstalk were alive to stop it rescheduling the job.     $pheanstalk-touch($job);     sleep(2);     // eventually were done, delete job.     $pheanstalk-delete($job); } catch(\Exception $e) {     // handle exception.     // and let some other worker retry.     $pheanstalk-release($job);  }   然后执行一下  php input.php 查看 状态 $ beanstool stats -------------------------------------------------------------------------------- | Name     | Buried   | Delayed  | Ready    | Reserved | Urgent   | Waiting  | Total    | -------------------------------------------------------------------------------- | default  | 0        | 0        | 0        | 0        | 0        | 0        | 0        | | testtube | 0        | 1        | 1        | 0        | 0        | 0        | 2        | --------------------------------------------------------------------------------   我们看到有一个在ready状态一个在delayed状态这是由于第二次的put采用了延时30s然后过一段时间后再看 $ beanstool stats -------------------------------------------------------------------------------- | Name     | Buried   | Delayed  | Ready    | Reserved | Urgent   | Waiting  | Total    | -------------------------------------------------------------------------------- | default  | 0        | 0        | 0        | 0        | 0        | 0        | 0        | | testtube | 0        | 0        | 2        | 0        | 0        | 0        | 2        | -------------------------------------------------------------------------------- 复制代码 已经有2个在ready状态了。 此时我们用消费者执行一下 php output.php  与此同时迅速看状态 复制代码 $ beanstool stats -------------------------------------------------------------------------------- | Name     | Buried   | Delayed  | Ready    | Reserved | Urgent   | Waiting  | Total    | -------------------------------------------------------------------------------- | default  | 0        | 0        | 0        | 0        | 0        | 0        | 0        | | testtube | 0        | 0        | 1        | 1        | 0        | 0        | 2        | -------------------------------------------------------------------------------- 再次执行 $ beanstool stats -------------------------------------------------------------------------------- | Name     | Buried   | Delayed  | Ready    | Reserved | Urgent   | Waiting  | Total    | -------------------------------------------------------------------------------- | default  | 0        | 0        | 0        | 0        | 0        | 0        | 0        | | testtube | 0        | 0        | 1        | 0        | 0        | 0        | 2        | -------------------------------------------------------------------------------- 复制代码 因为我们有sleep(2)所以要尽量快点操作这个状态监控的命令可以看到有一个拿出来放入了reserved然后就消失了(实际上这是后面的代码delete导致的因为已经消费完毕) 再次执行 php output.php  $ beanstool stats: -------------------------------------------------------------------------------- | Name     | Buried   | Delayed  | Ready    | Reserved | Urgent   | Waiting  | Total    | -------------------------------------------------------------------------------- | default  | 0        | 0        | 0        | 0        | 0        | 0        | 0        | | testtube | 0        | 0        | 0        | 1        | 0        | 0        | 2        | -------------------------------------------------------------------------------- $ beanstool stats: ------------------------------------------------------------------------------- | Name    | Buried   | Delayed  | Ready    | Reserved | Urgent   | Waiting  | Total    | ------------------------------------------------------------------------------- | default | 0        | 0        | 0        | 0        | 0        | 0        | 0        | -------------------------------------------------------------------------------   同样也是迅速观测这个状态发现消费1个然后删除1个现在队列空了这说明确实是符合我们的期望的。 然后回到文章开头提到的扩展这个扩展就是帮我们实现了composer装的那个pheanstalk包。 这个扩展如何安装呢 步骤如下 克隆项目 git clone https://gitee.com/qzfzz/php-beanstalk.git 进行编译  phpize 然后 ./configure 之后 make  make install sudo make install 然后修改 php.ini sudo gedit /etc/php/7.4/cli/php.ini /etc/php/7.4/apache2/php.ini 加上这句 extensionbeanstalk 重启apache2 sudo /etc/init.d/apache2 restart 之后就可以使用这个扩展了。 这个扩展它封装为函数了可以看到他有个例子文件 简单的找了几个例子 复制代码 ?php $config [   host 127.0.0.1,   port 11300 ]; $beanstalk_obj beanstalk_connect($config[host], $config[port]); $last_job_id beanstalk_put($beanstalk_obj, message detail); beanstalk_delete($beanstalk_obj, $last_job_id); $last_job_id beanstalk_putInTube($beanstalk_obj, tubea, message detail); 复制代码 可以看到使用 connect 连接 put 塞入新的job消息 putInTube 来塞入指定管道的tubeadelete来删除等等具体可以看看源代码学习一下我对比了一下这两种方式实现效率。 第一种采用composer包(我还特意去掉了加载文件所需要的时间) 复制代码 ?phprequire __DIR__ . /vendor/autoload.php; use Pheanstalk\Pheanstalk; $start microtime( true ); $pheanstalk Pheanstalk::create(127.0.0.1); $pheanstalk   -useTube(testtube)   -put(date(Y-m-d H:i:s) . job payload goes here\n); $end microtime(true); echo ($end - $start) * 1000 . ms; 复制代码 执行需要2.59ms 第二种直接用扩展函数 ?php $start microtime(true); $config [   host 127.0.0.1,   port 11300 ]; $beanstalk_object beanstalk_connect($config[host], $config[port]); $last_job_id beanstalk_putInTube($beanstalk_object, testtube, date(Y-m-d H:i:s) . job payload goes here\n); $end microtime(true); echo ($end - $start) * 1000 . ms; 复制代码 执行需要0.34ms 不得不说扩展就是扩展就是快的多啊 我另外测试了一下投递极限 循环投递10000次消息大概在500ms左右 复制代码 $start microtime( true ); for ($i0; $i 10000; $i) {    $pheanstalk   -useTube(testtube)   -put(date(Y-m-d H:i:s) . job payload goes here\n); } $end microtime( true ); echo ($end - $start) * 1000 . ms;   也就意味着1秒钟只能投递20000条消息这比rabbitmq的投递慢多了(参见这篇文章) 但是它执行1次投递消息的时候却比这个rabbitmq的代码执行的快原因是我测试了一下这个rabbitmq的连接上就耗费了9ms $connection new AMQPStreamConnection(localhost, 5672, guest, guest); 更别提还有这两步了 $channel $connection-channel(); $channel-queue_declare(hello, false, false, false, false); 这样来看想迅速投递一条短消息或者建立小信息量的job用beanstalk是很不错的如果有大量消息集中投递那使用 rabbitmq 是很不错的。 另外这个beanstalk投递可以延时非常适合有些时候需要在当前时间一段时间后执行某个任务往后弄个类似于一次性的定时器的功能这个东西值得尝试。 而且如果使用while死循环将 output.php 脚本一直挂着跑它就能一直消费消息了这样就等于有个后端进程一直能帮我们做消费者处理堆积的任务了特殊场景可以考虑一下这个方案。 Windows 安装 Beanstalkd 下载并安装 cygwin 1. 添加镜像地址 - http://mirrors.aliyun.com 2. 下载 gcc、gcc-core、make、automake, 在 Devel 分支下 下载 beanstalkd-win 包 1. 解压并进入beanstalkd-win目录 2. 打开CMD窗口,运行 ./beanstalkd.exe -l 127.0.0.1 -p 11300
http://www.zqtcl.cn/news/993854/

相关文章:

  • 触摸网站手机wordpress建立模板下载
  • 做暧在线观看网站网站建设与管理工资
  • 横岗网站建设无锡网站seo外包
  • 房管局 网站做房查学做网站推广要多久时间
  • 电脑网站开发者模式田园综合体建设网站
  • 南宁广告公司网站建设自适应网站建设模板
  • 做北京电梯招标的网站衡阳县专业做淘宝网站
  • 建设网站的语言wordpress主题自定义添加后台设置
  • 制造动漫网站开发目的四川酒店网站建设
  • 中国城市建设研究院深圳分院网站广西圣泰建设工程有限公司网站
  • 网站建设的方法有哪些内容wordpress展示插件
  • 北京手机网站制作公司wordpress 简易教程
  • 手机网站建站公司有哪些搜索引擎收录
  • 仿同程网 连锁酒店 网站模板学校网站建设用哪个系统
  • 教做甜品的网站删除wordpress主题字体载入
  • 做酒店网站所用到的算法wordpress侧栏导航
  • 做漫画的网站有哪些信息门户网站怎么做
  • 九江集团网站建设公司信誉好的广州做网站
  • 福州网站建设服务平台今天发生的重大新闻
  • 招聘信息网搜索引擎优化代理
  • 免费的企业网站cms纯文字logo在线制作
  • 深圳电器公司官网网站建设 网站优化
  • 大连 网站建设昆明建设网站哪家好
  • 网站首页设计及运行效果网站建设与管理任务分工
  • 自己建设论坛网站家用电脑搭建服务器
  • 做网站上海公司企业网站内页
  • 手机网站seo山东网站建设网
  • 溧阳 招网站开发wordpress 占内存
  • 网站seo 工具做网站建设公司排名
  • 丹阳网站建设企业建设网站管理制度