分布式任务系统gearman及python实例

Gearman是一个用来把工作委派给其他机器、分布式的调用更适合做某项工作的机器、并发的做某项工作在多个调用间做负载均衡、或用来在调用其它语言的函数的系统。Gearman是一个分发任务的程序框架,可以用在各种场合,开源、多语言支持、灵活、快速、可嵌入、可扩展、无消息大小限制、可容错,与Hadoop相比,Gearman更偏向于任务分发功能。它的任务分布非常简单,简单得可以只需要用脚本即可完成。Gea
摘要由CSDN通过智能技术生成

Gearman是一个用来把工作委派给其他机器、分布式的调用更适合做某项工作的机器、并发的做某项工作在多个调用间做负载均衡、或用来在调用其它语言的函数的系统。Gearman是一个分发任务的程序框架,可以用在各种场合,开源、多语言支持、灵活、快速、可嵌入、可扩展、无消息大小限制、可容错,与Hadoop相比,Gearman更偏向于任务分发功能。它的任务分布非常简单,简单得可以只需要用脚本即可完成。Gearman最初用于LiveJournal的图片resize功能,由于图片resize需要消耗大量计算资 源,因此需要调度到后端多台服务器执行,完成任务之后返回前端再呈现到界面。

gearman的任务传递模式是一对一的,不能实现一对多,一个client通过job server最后只能够到达一个worker上。如果需要一对多,需要定义多个worker的function,依次向这些worker进行发送,非常的不方便。这一点就不如ZeroMQ,ZeroMQ支持的模式很多,能够满足各种消息队列需求。他们用在不同的场合,Gearman是分布式任务系统,而ZeroMQ是分布式消息系统,任务只需要做一次就行。

1. Server

1.1 Gearman工作原理

Gearman 服务有很多要素使得它不仅仅是一种提交和共享工作的方式,但是主要的系统只由三个组件组成: gearmand 守护进程(server),用于向 Gearman 服务提交请求的 client ,执行实际工作的 worker。其关系如下图所示:


Gearmand server执行一个简单的功能,即从client收集job请求并充当一个注册器,而worker可以在此提交关于它们支持的job和操作类型的信息,这样server实际上就充当了Client和Worker的中间角色。Client将job直接丢给server,而server根据worker提交的信息,将这些job分发给worker来做,worker完成后也可返回结果,server将结果传回client。举个例子,在一个公司里面,有老板1、老板2、老板3(client),他们的任务就是出去喝酒唱歌拉项目(job),将拉来的项目直接交给公司的主管(server),而主管并不亲自来做这些项目,他将这些项目分给收手下的员工(worker)来做,员工做完工作后,将结果交给主管,主管将结果报告给老板们即可。

要使用gearman,首先得安装server,下载地址:https://launchpad.net/gearmand。当下载安装完成后,可以启动gearmand,启动有很多参数选项,可以man gearmand来查看,主要的 选项有:

  • -b, --backlog=BACKLOG       Number of backlog connections for listen. 
  • -d, --daemon                Daemon, detach and run in the background. 
  • -f, --file-descriptors=FDS  Number of file descriptors to allow for the process                             
  • (total connections will be slightly less). Default     is max allowed for user. 
  • -h, --help                  Print this help menu. 
  • -j, --job-retries=RETRIES   Number of attempts to run the job before the job  server removes it. Thisis helpful to ensure a bad  job does not crash all available workers. Default  is no limit. 
  • -l, --log-file=FILE         Log file to write errors and information to. Turning this option on also forces the first  verbose level to be enabled. 
  • -L, --listen=ADDRESS        Address the server should listen on. Default is  INADDR_ANY. 
  • -p, --port=PORT             Port the server should listen on. 
  • -P, --pid-file=FILE         File to write process ID out to. 
  • -r, --protocol=PROTOCOL     Load protocol module. 
  • -R, --round-robin           Assign work in round-robin order per  workerconnection. The default is to assign work in  the order of functions added by the worker. 
  • -q, --queue-type=QUEUE      Persistent queue type to use. 
  • -t, --threads=THREADS       Number of I/O threads to use. Default=0. 
  • -u, --user=USER             Switch to given user after startup. 
  • -v, --verbose               Increase verbosity level by one. 
  • -V, --version               Display the version of gearmand and exit. 
  • -w, --worker-wakeup=WORKERS Number of workers to wakeup for each job received.   The default is to wakeup all available workers.

启动gearmand:

sudo gearmand --pid-file=/var/run/gearmand/gearmand.pid --daemon --log-file=/var/log/gearman.log
若提示没有/var/log/gearman.log这个文件的话,自己新建一个就可以了。


1.2 实例化queue与容错

Gearman默认是将queue保存在内存中的,这样能够保障速速,但是遇到宕机或者server出现故障时,在内存中缓存在queue中的任务将会丢失。Gearman提供了了queue实例化的选项,能够将queue保存在数据库中,比如:SQLite3、Drizzle、MySQL、PostgresSQL、Redis(in dev)、MongoDB(in dev).在执行任务前,先将任务存入持久化队列中,当执行完成后再将该任务从持久化队列中删除。要使用db来实例化queue,除了在启动时加入-q参数和对应的数据库之外,还需要根据具体的数据库使用相应的选项,例如使用sqlit3来实例化queue,并指明使用用来存储queue的文件:

gearmand -d -q libsqlite3 --libsqlite3-db=/tmp/demon/gearman.db --listen=localhost --port=4370

再如使用mysql来实例化queue,选项为:

<pre name="code" class="plain">/usr/local/gearmand/sbin/gearmand  -d  -u root \
–queue-type=MySQL \
–mysql-host=localhost \
–mysql-port=3306 \
–mysql-user=gearman \
–mysql-password=123456 \
–mysql-db=gearman \
–mysql-table=gearman_queue
 

还要创建相应的数据库和表,并创建gearman用户,分配相应的权限:

CREATE DATABASE gearman;
CREATE TABLE `gearman_queue` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`unique_key` varchar(64) NOT NULL,
`function_name` varchar(255) NOT NULL,
`when_to_run` int(10) NOT NULL,
`priority` int(10) NOT NULL,
`data` longblob NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_key_index` (`unique_key`,`function_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

create USER gearman@localhost identified by ’123456′;
GRANT ALL on gearman.* to gearman@localhost;

可以在gearman的配置文件中加入相关配置,以免每次启动都需要写一堆东西:

# /etc/conf.d/gearmand: config file for /etc/init.d/gearmand

# Persistent queue store
# The following queue stores are available:
# drizzle|memcache|mysql|postgre|sqlite|tokyocabinet|none
# If you do not wish to use persistent queues, leave this option commented out.
# Note that persistent queue mechanisms are mutally exclusive.
PERSISTENT="mysql"

# Persistent queue settings for drizzle, mysql and postgre
#PERSISTENT_SOCKET=""
PERSISTENT_HOST="localhost"
PERSISTENT_PORT="3306"
PERSISTENT_USER="gearman"
PERSISTENT_PASS="your-pass-word-here"
PERSISTENT_DB="gearman"
PERSISTENT_TABLE="gearman_queue"

# Persistent queue settings for sqlite
#PERSISTENT_FILE=""

# Persistent queue settings for memcache
#PERSISTENT_SERVERLIST=""

# General settings
#
# -j, --job-retries=RETRIES   Number of attempts to run the job before the job
#                             server removes it. Thisis helpful to ensure a bad
#                             job does not crash all available workers. Default
#                             is 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值