gearman安装

来源:互联网 发布:友情卡片 知乎 编辑:程序博客网 时间:2024/04/28 21:55

gearman安装

wget https://launchpadlibrarian.net/165674261/gearmand-1.1.12.tar.gztar zxf gearmand-1.1.12.tar.gzcd  gearmand-1.1.12./configure --prefix=/usr/local/webserver/gearman yum install boost-devel.x86_64 boost -y yum install gperf.x86_64 -y yum install libuuid-devel -y ./configure --prefix=/usr/local/webserver/gearman

结束后会提示支持哪些持久化机制
安安装图

make出错了应该是libevent版本低了升高版本就好了make && make install[root@localhost gearman-1.1.2]# ln -s /usr/local/webserver/gearman/lib/libgearman.so.8.0.0 /usr/lib64/libgearman.so[root@localhost gearman-1.1.2]# ln -s /usr/local/webserver/gearman/lib/libgearman.so.8.0.0 /usr/lib64/libgearman.so.8gearmand -L 192.168.100.204 -p 4730 -u root -d

启动参数说明:

-b, --backlog= 储备的监听连接数量-d, --daemon 后台运行-f, --file-descriptors= 文件描述符的数量-h, --help 帮助-j, --job-retries= 在job server移除不可用job之前运行的次数,防止不断运行导致其他可用worker崩溃。默认没有限制-l, -log-file= 日志文件存放位置(默认记录最简单日志)-L, --listen= 监听的IP,默认全部接受-p, --port= 指定监听端口-P, --pid-file= 指定进程ID写入位置-r, --protocol= 加载协议模块-q, --queue-type= 指定持久化队列-t, --threads= 使用的I/O线程数量。默认为0-u, --user= 启动后,切换到指定用户-v, --verbose 增加一级详细程度-V, --version 显示版本信息

php拓展

wget http://pecl.php.net/get/gearman-1.1.2.tgztar zxf gearman-1.1.2.tgzcd gearman-1.1.2phpize ./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-gearman=/usr/local/webserver/gearman/export GEARMAN_LIB_DIR=/usr/local/webserver/gearman/lib/yum install libgearman-develhttp://fr2.rpmfind.net/linux/rpm2html/search.php?query=libgearman-devel&system=&arch=python拓展https://pypi.python.org/pypi  下载setuptoolspython setup.py install  安装然后下载http://gearman.org/download/#python安装gearman-pythonpython setup.py install  安装测试编写 Worker     worker.php 文件内容如下:<?php$worker= new GearmanWorker();$worker->addServer('127.0.0.1', 4730);$worker->addFunction('reverse', 'my_reverse_function');while ($worker->work());function my_reverse_function($job) {return strrev($job->workload());}?>                    设置后台运行 work                    php worker.php &编写 Client     client.php 文件内容如下:<?php$client= new GearmanClient();$client->addServer('127.0.0.1', 4730);echo $client->doNormal('reverse', 'Hello World!'), "\n";?>运行 client     php client.php     输出:!dlroW olleH出于方便的考虑,Worker,Client 使用的都是PHP,但这并不影响演示,实际应用中,你完全可以通过 Gearman 集成不同语言实现的 Worker,Client。或许此时你还想了解前面提到的负载均衡功能:很简单,只要增加多个 Worker 即可,你可以按照 worker.php 的样子多写几个类似的文件,并设置不同的返回值用以识别演示效果。然后依次启动这几个 Worker 文件,并多次使用 client.php 去请求,你就会发现 Job 会把 Client 请求转发给不同的 Worker。命令行工具如果你觉得安装 PHP 之类的东西太麻烦的话,你也可以仅仅通过命令行工具来体验 Gearman 的功能:启动 Worker:gearman -w -f wc -- wc -l &运行 Client:gearman -f wc < /etc/passwd具体可以参考官方文档,还有一些不错的 PDF测试pythonwork编写import osimport gearmanimport mathclass CustomGearmanWorker(gearman.GearmanWorker):    def on_job_execute(self, current_job):        print "Job started"        return super(CustomGearmanWorker, self).on_job_execute(current_job)def task_callback(gearman_worker, job):    print job.data    return job.datanew_worker = CustomGearmanWorker(['127.0.0.1:4730'])new_worker.register_task("echo", task_callback)new_worker.work()client编写          单任务from gearman import GearmanClientimport os,sysnew_client = GearmanClient(['127.0.0.1:4730'])current_request = new_client.submit_job('echo', sys.argv[1])new_result = current_request.resultprint new_result多任务from gearman import GearmanClientimport os,sysnew_client = GearmanClient(['127.0.0.1:4730'])new_jobs = [    dict(task='echo', data=sys.argv[1]),    dict(task='echo', data=sys.argv[2]),]completed_requests = new_client.submit_multiple_jobs(new_jobs)for current_request in completed_requests:    assert current_request.result == current_request.job.data

这里写图片描述
work端看
这里写图片描述

shell命令

Common options to both client and worker modes.        -f <function> - Function name to use for jobs (can give many)处理任务的函数名        -h <host>     - Job server host  (Job Server主机,默认是localhost)        -H            - Print this help menu        -p <port>     - Job server port (Job Server端口,默认是4730        -t <timeout>  - Timeout in milliseconds  (执行多长时间超时,微秒)        -i <pidfile>  - Create a pidfile for the process (创建进程的pid文件)Client部分参数Client options:        -b            - Run jobs in the background (后台运行任务)        -I            - Run jobs as high priority (高优先级运行任务)        -L            - Run jobs as low priority (低优先级运行任务)        -n            - Run one job per line (逐行执行任务)        -N            - Same as -n, but strip off the newline        -P            - Prefix all output lines with functions names (在输入结果前面加处理的函数名)        -s            - Send job without reading from standard input 执行任务不返回结果        -u <unique>   - Unique key to use for job 任务的唯一标识Worker部分参数Worker options:        -c <count>    - Number of jobs for worker to run before exiting (统计worker进程处理多少个任务后中止)        -n            - Send data packet for each line        -N            - Same as -n, but strip off the newline        -w            - Run in worker mode   以worker模式运行
0 0