gearman 怎么与 tornado 异步请求结合达到异步无阻塞

gearman异步队列安装及使用教程 - 推酷
gearman异步队列安装及使用教程
o 以下测试数值为虚拟机
o 常规HTTP的Curl:这个受端口最大值及打开文件句柄数限制
o 当使用HTTP Curl时
o 同步:2000/sec
o Gearman:未优化情况下
o 用PHP+gearman揑件测试并发批量调用:
o 同步:4000/sec
o 异步:10000/sec
o 以上还有上升空间,官方测试数据为5w/sec
o 增加持久化揑件设置后性能会下降一些
gearman异步队列安装流程
必备条件:你的centos需要提前安装了lnmp,如果没有安装可以参照教程:
http://blog.csdn.net/e/article/details/
http://blog.csdn.net/e/article/details/
基础安装包
yum install vim wget gcc gcc-c++ make dos2unix gperf libevent libevent-devel zlib-devel bzip2-devel openssl-devel ncurses-devel
boost boost-devel mysql-devel
安装gearman 异步队列
# wget https://launchpad.net/gearmand/1.2/1.1.9/+download/gearmand-1.1.9.tar.gz
# tar -zxvf gearmand-1.1.9.tar.gz
# cd gearmand-1.1.9
# ./configure
如果出现错误请查看下面的错误解决
成功后如下
* LDFLAGS Flags:
* Assertions enabled:
* Debug enabled:
* Warnings as failure:
* Building with libsqlite3
* Building with libdrizzle
* Building with libmemcached not found
* Building with libpq
* Building with tokyocabinet no
* Building with libmysql
* SSL enabled:
* make -j:
* VCS checkout:
make install
安装gearman php 扩展
# wget http://pecl.php.net/get/gearman
# mv gearman gearman.tar.gz
# tar -zxvf gearman.tar.gz
# cd gearman-1.1.2/
# ./configure
# make install
# cd /etc/php.d/
# cp gd.ini gearman.ini
# vim gearman.ini
; Enable gearman extension module
extension=gearman.so
# service php-fpm restart
在configure过程中出现了以下错误:
checking for Boost headers version &= 1.39.0… no
configure: error: cannot find Boost headers version &= 1.39.0
解决办法:
# yum search boost
# yum install boost.x86_64
# yum install boost-devel.x86_64
继续执行./configure出现以下错误
checking for gperf... no
configure: error: could not find gperf
解决办法:
#yum search gperf
#yum install gperf.x86_64
继续执行./configure出现以下错误
checking test for a working libevent... no
configure: error: Unable to find libevent
解决办法:
# yum install libevent libevent-devel
gearman 参数说明
Client mode: gearman [options] [&data&]
Worker mode: gearman -w [options] [&command& [&args& ...]]
Common options to both client and worker modes.
-f &function& - Function name to use for jobs (can give many)
- Job server host
- Print this help menu
- Print diagnostic information to stdout(false)
- Job server port
-t &timeout&
- Timeout in milliseconds
-i &pidfile&
- Create a pidfile for the process
Client options:
- Run jobs in the background(false)
- Run jobs as high priority
- Run jobs as low priority
- Run one job per line(false)
- Same as -n, but strip off the newline(false)
- Prefix all output lines with functions names
- Send job without reading from standard input
-u &unique&
- Unique key to use for job
Worker options:
-c &count& - Number of jobs for worker to run before exiting
- Send data packet for each line(false)
- Same as -n, but strip off the newline(false)
- Run in worker mode(false)
gearman异步队列使用:
下面先做个命令行测试:
首先开两个命令行窗口:
# gearman -w -f abc
表示统计用户输入了多少个字符。
# gearman -f abc 'aaaa'
输出结果正确。
# gearman -f abc & /etc/php.ini
当然也可以直接从文件中读入内容。
下面使用php做同步列队测试:
# vi /var/www/html/company/gearman/worker.php
只要能保证nginx能访问到就可以
$worker= new GearmanWorker();
$worker-&addServer('127.0.0.1', 4730); //连接job服务器
$worker-&addFunction('reverse', 'my_reverse_function');
//注册支持任务及对应函数
while ($worker-&work()); //循环等待任务,没有时阻塞,循环体内可以放错误处理
//work内尽量不要出现资源忘记回收情况
//处理任务的回调函数
function my_reverse_function($job)
$workload = $job-&workload();
//过来的参数
$result = strrev($workload); //运算
$content = file_get_contents(&&); //加大运算时间
//加入执行日志
$file = fopen(&worker_counter.log&,&a+&);
fwrite($file,date(&Y-m-d H:i:s&).&\n&);
//fwrite($file,var_export($job,TRUE).&\n&);
fwrite($file,$workload.&\n&);
fwrite($file,$result.&\n&);
fclose($file);
//返回给调用方的数据
# vi /var/www/html/company/gearman/client.php
只要能保证nginx能访问到就可以
$client= new GearmanClient();
$client-&addServer('127.0.0.1', 4730);
echo $client-&do('reverse', 'You can do it.'), &\n&;
}catch(Exception $e){
print_r($e);
使用tty1运行worker
# php worker.php
使用tty2运行client
# php client.php
.ti od nac uoY
输出结果正确
使用浏览器运行client
http://192.168.252.128/company/gearman/client.php
.ti od nac uoY
输出结果正确
下面使用php做异步列队测试:
# vi /var/www/html/company/gearman/worker.php
只要能保证nginx能访问到就可以
$worker= new GearmanWorker();
$worker-&addServer('127.0.0.1', 4730);
$worker-&addFunction('reverse', 'my_reverse_function');
while ($worker-&work());
function my_reverse_function($job)
$workload = $job-&workload();
$result = strrev($workload);
$content = file_get_contents(&&); //加大运算时间
//加入执行日志
$file = fopen(&worker_counter.log&,&a+&);
fwrite($file,date(&Y-m-d H:i:s&).&\n&);
//fwrite($file,var_export($job,TRUE).&\n&);
fwrite($file,$workload.&\n&);
fwrite($file,$result.&\n&);
fclose($file);
# vi /var/www/html/company/gearman/client.php
只要能保证nginx能访问到就可以
$client= new GearmanClient();
$client-&addServer('127.0.0.1', 4730);
echo $client-&doBackground('reverse', 'You can do it.'), &\n&; //异步只是派发任务,不等待返回结果
}catch(Exception $e){
print_r($e);
使用tty1运行worker
# php worker.php
使用tty2查看执行效果
# tail -f /var/www/html/company/gearman/worker_counter.log
使用tty3运行client
# php client.php
H:localhost:150
表示该操作列队中,我可以在tty2中实时查看到效果
You can do it.
.ti od nac uoY
使用浏览器运行client
http://192.168.252.128/company/gearman/client.php
H:localhost:153
表示该操作列队中,我可以在tty2中实时查看到效果
You can do it.
.ti od nac uoY
我们看到了client不管执行任何操作,都会立即得到一个列队ID,剩余的操作交给了worker。
详细使用参考文档:
/link?url=YDJGOfRtZ9ySNsYqggK6lPhFqaSCsRb9NskUVE5jwpECH7qR58lyY7m3rutAEq1l2Ma--mOHe-o5YE0oJSSTUd16jcABeSwJNdu4V14N16a
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
没有分页内容
图片无法显示
视频无法显示
与原文不一致? 以下测试数值为虚拟机
? 常规HTTP的Curl:这个受端口最大值及打开文件句柄数限制
? 当使用HTTP Curl时
? 同步:2000/sec
? Gearman:未优化情况下
? 用PHP+gearmanI件测试并发批量调用:
? 同步:4000/sec
? 异步:10000/sec
? 以上还有上升空间,官方测试数据为5w/sec
? 增加持久化I件设置后xìng能会下降一些
gearman异步队列安装流程
必备条件:你的centos需要提前安装了lnmp,如果没有安装可以参照教程:
http://blog.csdn.net/e/article/details/
http://blog.csdn.net/e/article/details/
基础安装包
yum install vim wget gcc gcc-c++ make dos2unix gperf libevent libevent-devel zlib-devel bzip2-devel openssl-devel ncurses-devel
boost boost-devel mysql-devel
安装gearman 异步队列
# wget https://launchpad.net/gearmand/1.2/1.1.9/+download/gearmand-1.1.9.tar.gz
# tar -zxvf gearmand-1.1.9.tar.gz
# cd gearmand-1.1.9
# ./configure
如果出现错误请查看下面的错误解决
成功后如下
* LDFLAGS Flags:
* Assertions enabled:
* Debug enabled:
* Warnings as failure:
* Building with libsqlite3
* Building with libdrizzle
* Building with libmemcached not found
* Building with libpq
* Building with tokyocabinet no
* Building with libmysql
* SSL enabled:
* make -j:
* VCS checkout:
make install
安装gearman php 扩展
# wget http://pecl.php.net/get/gearman
# mv gearman gearman.tar.gz
# tar -zxvf gearman.tar.gz
# cd gearman-1.1.2/
# ./configure
# make install
# cd /etc/php.d/
# cp gd.ini gearman.ini
# vim gearman.ini
; Enable gearman extension module
extension=gearman.so
# service php-fpm restart
在configure过程中出现了以下错误:
checking for Boost headers version &= 1.39.0… no
configure: error: cannot find Boost headers version &= 1.39.0
解决办法:
# yum search boost
# yum install boost.x86_64
# yum install boost-devel.x86_64
继续执行./configure出现以下错误
checking for gperf... no
configure: error: could not find gperf
解决办法:
#yum search gperf
#yum install gperf.x86_64
继续执行./configure出现以下错误
checking test for a working libevent... no
configure: error: Unable to find libevent
解决办法:
# yum install libevent libevent-devel
gearman 参数说明
Client mode: gearman [options] [&data&]
Worker mode: gearman -w [options] [&command& [&args& ...]]
Common options to both client and worker modes.
-f &function& - Function name to use for jobs (can give many)
- Job server host
- Print this help menu
- Print diagnostic information to stdout(false)
- Job server port
-t &timeout&
- Timeout in milliseconds
-i &pidfile&
- Create a pidfile for the process
Client options:
- Run jobs in the background(false)
- Run jobs as high priority
- Run jobs as low priority
- Run one job per line(false)
- Same as -n, but strip off the newline(false)
- Prefix all output lines with functions names
- Send job without reading from standard input
-u &unique&
- Unique key to use for job
Worker options:
-c &count&
- Number of jobs for worker to run before exiting
- Send data packet for each line(false)
- Same as -n, but strip off the newline(false)
- Run in worker mode(false)
gearman异步队列使用:
下面先做个命令行测试:
首先开两个命令行窗口:
# gearman -w -f abc
表示统计用户输入了多少个字符。
# gearman -f abc 'aaaa'
输出结果正确。
# gearman -f abc & /etc/php.ini
当然也可以直接从文件中读入内容。
下面使用php做同步列队测试:
# vi /var/www/html/company/gearman/worker.php
只要能保证nginx能访问到就可以
$worker= new GearmanWorker();
$worker-&addServer('127.0.0.1', 4730); //连接job服务器
$worker-&addFunction('reverse', 'my_reverse_function');
//注册支持任务及对应函数
while ($worker-&work());
//循环等待任务,没有时阻塞,循环体内可以放错误chǔ理
//work内尽量不要出现资源忘记回收情况
//chǔ理任务的回调函数
function my_reverse_function($job)
$workload = $job-&workload();
//过来的参数
$result = strrev($workload);
$content = file_get_contents(&&);&& &//加大运算时间
//加入执行日志
$file = fopen(&worker_counter.log&,&a+&);
fwrite($file,date(&Y-m-d H:i:s&).&\n&);
//fwrite($file,var_export($job,TRUE).&\n&);
fwrite($file,$workload.&\n&);
fwrite($file,$result.&\n&);
fclose($file);
//返回给调用方的数据
# vi /var/www/html/company/gearman/client.php
只要能保证nginx能访问到就可以
$client= new GearmanClient();
$client-&addServer('127.0.0.1', 4730);
echo $client-&do('reverse', 'You can do it.'), &\n&;
}catch(Exception $e){
print_r($e);
使用tty1运行worker
# php worker.php
使用tty2运行client
# php client.php
.ti od nac uoY
输出结果正确
使用浏览器运行client
http://192.168.252.128/company/gearman/client.php
.ti od nac uoY
输出结果正确
下面使用php做异步列队测试:
# vi /var/www/html/company/gearman/worker.php
只要能保证nginx能访问到就可以
$worker= new GearmanWorker();
$worker-&addServer('127.0.0.1', 4730);
$worker-&addFunction('reverse', 'my_reverse_function');
while ($worker-&work());
function my_reverse_function($job)
$workload = $job-&workload();
$result = strrev($workload);
$content = file_get_contents(&&);
//加大运算时间
//加入执行日志
$file = fopen(&worker_counter.log&,&a+&);
fwrite($file,date(&Y-m-d H:i:s&).&\n&);
//fwrite($file,var_export($job,TRUE).&\n&);
fwrite($file,$workload.&\n&);
fwrite($file,$result.&\n&);
fclose($file);
# vi /var/www/html/company/gearman/client.php
只要能保证nginx能访问到就可以
$client= new GearmanClient();
$client-&addServer('127.0.0.1', 4730);
echo $client-&doBackground('reverse', 'You can do it.'), &\n&;
//异步只是派发任务,不等待返回结果
}catch(Exception $e){
print_r($e);
使用tty1运行worker
# php worker.php
使用tty2查看执行效果
# tail -f /var/www/html/company/gearman/worker_counter.log
使用tty3运行client
# php client.php
H:localhost:150
表示该操作列队中,我可以在tty2中实时查看到效果
You can do it.
.ti od nac uoY
使用浏览器运行client
http://192.168.252.128/company/gearman/client.php
H:localhost:153
表示该操作列队中,我可以在tty2中实时查看到效果
You can do it.
.ti od nac uoY
我们看到了client不管执行任何操作,都会立即得到一个列队ID,剩余的操作jiāo给了worker。
详细使用参考文档:
/link?url=YDJGOfRtZ9ySNsYqggK6lPhFqaSCsRb9NskUVE5jwpECH7qR58lyY7m3rutAEq1l2Ma--mOHe-o5YE0oJSSTUd16jcABeSwJNdu4V14N16a
&此文从网络中自动搜索生成,不代表本网站赞成被搜索网站的内容或立场
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&& 13:00:58
软件世界网- &2014 蜀ICP备号 三峰网旗下网站知乎为什么要选择用Tornado做为web开发框架,异步非阻塞模式在此起到了作用?
欢迎砖拍!
按投票排序
首先弄清楚,Tornado不只是个web框架,同时也是个http server。你可以只用它的web部分(还只有同步部分),当作个wsgi app使,这个时候拿来跟django啊flask啊做比较是相当的;你也可以拿它的httpserver部分来用,这时候他是可以做到跟nginx/apache相当的。当然,出于负载均衡的考虑,有可能在多台机器多个tornado进程之前还有个nginx做负载均衡工作,需要监控每一个进程而不能使用tornado自带多进程控制,然而并不需要再通过wsgi包裹,直接做反向代理就可以起到足够的效果,这便是与其它python系框架最大的不同。由于自身就是个http server,这使得完整的异步化流程得以实现,并且对多个请求发至同一个tornado进程的情况可以充分利用协程带来的轻量化上下文切换。前者为websocket和长链接提供了便利,后者为高性能打下了基础。对于没有大量计算的业务逻辑型后段而言,Tornado的性能甚至不输nodejs,并且语言和环境的成熟度优于node。这里说的性能可能并不全是指每秒请求数:单进程每秒次请求处理这一点很多框架都可以做到,而单进程个长链接,这就不是每个每个框架都可以做好的事情了。此外,并不是说用了Tornado,其它的东西就不能用了。Tornado前置负载均衡,后带wsgi也行,带内网Socket结合其它平台的分布式架构也行,扩展也有好处啊……
只要是知乎想用长连接推送功能,python里面tornado估计是唯一比较好的选择1.FriendFeed的成功证明了tornado,而tornado是FriendFeed发明的2.要用django,flask等必须借助gevent等,没有类似案例,风险太大(不稳定)3.要想用长连接,必须是异步非阻塞模式,不然一台机器支撑人数少的可怜当然nodejs,php(swole)也是可以的,估计知乎看不上,人生苦短,我用python。
私以为,真相很可能是当初主程对tornado最熟。
的答案很完善了,我觉得针对他的第三点而言,退回到几年前知乎刚开始的时候,2011年,什么nodejs啊,swoole远没现在成熟,tornado是最佳的选择吧,对于一个初创项目而言。
性能好到不像Python,核心逻辑很简单,没有其他什么功能,可以根据自己的业务来定制。我最讨厌的就是一大堆功能而且不能拆分的东西
已有帐号?
无法登录?
社交帐号登录

我要回帖

更多关于 tornado mysql 异步 的文章

 

随机推荐