1. yum -y install gearmand
  2. chkconfig gearmand on && /etc/init.d/gearmand start
  3. # /etc/sysconfig/gearmand
  4. 指定 OPTIONS=""里的内容,添加持久性等功能

example

worker.py

  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8; py-indent-offset: 4 -*-
  3. import gearman
  4. gm_worker = gearman.GearmanWorker(['localhost:4730'])
  5. def task_listener_reverse(gearman_worker, gearman_job):
  6. print 'Reversing string: ' + gearman_job.data
  7. return gearman_job.data[::-1]
  8. # gm_worker.set_client_id is optional
  9. gm_worker.set_client_id('python-worker')
  10. gm_worker.register_task('reverse', task_listener_reverse)
  11. # Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
  12. gm_worker.work()

client.py

  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8; py-indent-offset: 4 -*-
  3. import gearman
  4. def check_request_status(job_request):
  5. if job_request.complete:
  6. print "Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
  7. elif job_request.timed_out:
  8. print "Job %s timed out!" % job_request.unique
  9. elif job_request.state == JOB_UNKNOWN:
  10. print "Job %s connection failed!" % job_request.unique
  11. gm_client = gearman.GearmanClient(['localhost:4730'])
  12. completed_job_request = gm_client.submit_job("reverse", "Hello World!")
  13. check_request_status(completed_job_request)

results

  1. python client.py
  2. # Job b5175e15d8e09ba6470cbedb4b6e2b1a finished! Result: COMPLETE - !dlroW olleH
  3. python worker.py
  4. # Reversing string: Hello World!

API

Gearman worker

  1. class gearman.worker.GearmanWorker(host_list=None)
  2. GearmanWorker :: Interface to accept jobs from a Gearman server

1 Job processing

  1. GearmanWorker.set_client_id(client_id)
  2. # Notify the server that we should be identified as this client ID
  3. GearmanWorker.register_task(task, callback_function)
  4. # Register a function with this worker
  5. def function_callback(calling_gearman_worker, current_job):
  6. return current_job.data
  7. GearmanWorker.unregister_task(task)
  8. # Unregister a function with worker
  9. GearmanWorker.work(poll_timeout=60.0)
  10. # Loop indefinitely, complete tasks from all connections.
  1. # example
  2. gm_worker = gearman.GearmanWorker(['localhost:4730'])
  3. # See gearman/job.py to see attributes on the GearmanJob
  4. # Send back a reversed version of the 'data' string
  5. def task_listener_reverse(gearman_worker, gearman_job):
  6. return reversed(gearman_job.data)
  7. # gm_worker.set_client_id is optional
  8. gm_worker.set_client_id('your_worker_client_id_name')
  9. gm_worker.register_task('reverse', task_listener_reverse)
  10. # Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
  11. gm_worker.work()

2 Sending in-flight job updates

  1. GearmanWorker.send_job_data(current_job, data, poll_timeout=None)
  2. # Send a Gearman JOB_DATA update for an inflight job
  3. GearmanWorker.send_job_status(current_job, numerator, denominator, poll_timeout=None)
  4. # Send a Gearman JOB_STATUS update for an inflight job
  5. GearmanWorker.send_job_warning(current_job, data, poll_timeout=None)
  6. # Send a Gearman JOB_WARNING update for an inflight job
  1. # example
  2. Callback function sending back inflight job updates:
  3. gm_worker = gearman.GearmanWorker(['localhost:4730'])
  4. # See gearman/job.py to see attributes on the GearmanJob
  5. # Send back a reversed version of the 'data' string through WORK_DATA instead of WORK_COMPLETE
  6. def task_listener_reverse_inflight(gearman_worker, gearman_job):
  7. reversed_data = reversed(gearman_job.data)
  8. total_chars = len(reversed_data)
  9. for idx, character in enumerate(reversed_data):
  10. gearman_worker.send_job_data(gearman_job, str(character))
  11. gearman_worker.send_job_status(gearman_job, idx + 1, total_chars)
  12. return None
  13. # gm_worker.set_client_id is optional
  14. gm_worker.register_task('reverse', task_listener_reverse_inflight)
  15. # Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
  16. gm_worker.work()

3 Extending the worker

  1. GearmanWorker.data_encoder
  2. # Provide common object dumps for all communications over gearman
  3. GearmanWorker.after_poll(any_activity)
  4. # Polling callback to notify any outside listeners whats going on with the GearmanWorker.
  5. Return True to continue polling, False to exit the work loop
  1. # example
  2. Send/receive Python objects and do work between polls:
  3. # By default, GearmanWorker's can only send off byte-strings
  4. # If we want to be able to send out Python objects, we can specify a data encoder
  5. # This will automatically convert byte strings <-> Python objects for ALL commands that have the 'data' field
  6. #
  7. # See http://gearman.org/index.php?id=protocol for Worker commands that send/receive 'opaque data'
  8. #
  9. import json # Or similarly styled library
  10. class JSONDataEncoder(gearman.DataEncoder):
  11. @classmethod
  12. def encode(cls, encodable_object):
  13. return json.dumps(encodable_object)
  14. @classmethod
  15. def decode(cls, decodable_string):
  16. return json.loads(decodable_string)
  17. class DBRollbackJSONWorker(gearman.GearmanWorker):
  18. data_encoder = JSONDataEncoder
  19. def after_poll(self, any_activity):
  20. # After every select loop, let's rollback our DB connections just to be safe
  21. continue_working = True
  22. self.db_connections.rollback()
  23. return continue_working

Gearman client

  1. def check_request_status(job_request):
  2. if job_request.complete:
  3. print "Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
  4. elif job_request.timed_out:
  5. print "Job %s timed out!" % job_request.unique
  6. elif job_request.state == JOB_UNKNOWN:
  7. print "Job %s connection failed!" % job_request.unique
  8. class gearman.client.GearmanClient(host_list=None, random_unique_bytes=16)
  9. # GearmanClient :: Interface to submit jobs to a Gearman server

1 Submitting jobs

  1. GearmanClient.submit_job(task, data, unique=None, priority=None, background=False, wait_until_complete=True, max_retries=0, poll_timeout=None)
  2. # Submit a single job to any gearman server
  1. # example
  2. Sending a simple job as a blocking call:
  3. gm_client = gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])
  4. # See gearman/job.py to see attributes on the GearmanJobRequest
  5. # Defaults to PRIORITY_NONE, background=False (synchronous task), wait_until_complete=True
  6. completed_job_request = gm_client.submit_job("task_name", "arbitrary binary data")
  7. check_request_status(completed_job_request)
  8. Sending a high priority, background, blocking call:
  9. gm_client = gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])
  10. # See gearman/job.py to see attributes on the GearmanJobRequest
  11. submitted_job_request = gm_client.submit_job("task_name", "arbitrary binary data", priority=gearman.PRIORITY_HIGH, background=True)
  12. check_request_status(submitted_job_request)
  1. GearmanClient.submit_multiple_jobs(jobs_to_submit, background=False, wait_until_complete=True, max_retries=0, poll_timeout=None)
  2. Takes a list of jobs_to_submit with dicts of
  3. {‘task’: task, data’: data, unique’: unique, priority’: priority}
  1. # example
  2. Sending multiple jobs all at once and behave like a non-blocking call (wait_until_complete=False):
  3. import time
  4. gm_client = gearman.GearmanClient(['localhost:4730'])
  5. list_of_jobs = [dict(task="task_name", data="binary data"), dict(task="other_task", data="other binary data")]
  6. submitted_requests = gm_client.submit_multiple_jobs(list_of_jobs, background=False, wait_until_complete=False)
  7. # Once we know our jobs are accepted, we can do other stuff and wait for results later in the function
  8. # Similar to multithreading and doing a join except this is all done in a single process
  9. time.sleep(1.0)
  10. # Wait at most 5 seconds before timing out incomplete requests
  11. completed_requests = gm_client.wait_until_jobs_completed(submitted_requests, poll_timeout=5.0)
  12. for completed_job_request in completed_requests:
  13. check_request_status(completed_job_request)
  1. GearmanClient.submit_multiple_requests(job_requests, wait_until_complete=True, poll_timeout=None)
  2. # Take GearmanJobRequests, assign them connections, and request that they be done.
  3. Blocks until our jobs are accepted (should be fast) OR times out
  4. Optionally blocks until jobs are all complete
  5. You MUST check the status of your requests after calling this function as timed_out or state == JOB_UNKNOWN maybe True
  1. # example
  2. Recovering from failed connections:
  3. import time
  4. gm_client = gearman.GearmanClient(['localhost:4730'])
  5. list_of_jobs = [dict(task="task_name", data="task binary string"), dict(task="other_task", data="other binary string")]
  6. failed_requests = gm_client.submit_multiple_jobs(list_of_jobs, background=False)
  7. # Let's pretend our assigned requests' Gearman servers all failed
  8. assert all(request.state == JOB_UNKNOWN for request in failed_requests), "All connections didn't fail!"
  9. # Let's pretend our assigned requests' don't fail but some simply timeout
  10. retried_connection_failed_requests = gm_client.submit_multiple_requests(failed_requests, wait_until_complete=True, poll_timeout=1.0)
  11. timed_out_requests = [job_request for job_request in retried_requests if job_request.timed_out]
  12. # For our timed out requests, lets wait a little longer until they're complete
  13. retried_timed_out_requests = gm_client.submit_multiple_requests(timed_out_requests, wait_until_complete=True, poll_timeout=4.0)
  1. GearmanClient.wait_until_jobs_accepted(job_requests, poll_timeout=None)
  2. # Go into a select loop until all our jobs have moved to STATE_PENDING
  1. GearmanClient.wait_until_jobs_completed(job_requests, poll_timeout=None)
  2. # Go into a select loop until all our jobs have completed or failed

2 Retrieving job status

  1. GearmanClient.get_job_status(current_request, poll_timeout=None)
  2. # Fetch the job status of a single request
  3. GearmanClient.get_job_statuses(job_requests, poll_timeout=None)
  4. #Fetch the job status of a multiple requests

3 Extending the client

  1. GearmanClient.data_encoder
  2. # Provide common object dumps for all communications over gearman
  1. # example
  2. Send/receive Python objects (not just byte strings):
  3. # By default, GearmanClient's can only send off byte-strings
  4. # If we want to be able to send out Python objects, we can specify a data encoder
  5. # This will automatically convert byte strings <-> Python objects for ALL commands that have the 'data' field
  6. #
  7. # See http://gearman.org/index.php?id=protocol for client commands that send/receive 'opaque data'
  8. import pickle
  9. class PickleDataEncoder(gearman.DataEncoder):
  10. @classmethod
  11. def encode(cls, encodable_object):
  12. return pickle.dumps(encodable_object)
  13. @classmethod
  14. def decode(cls, decodable_string):
  15. return pickle.loads(decodable_string)
  16. class PickleExampleClient(gearman.GearmanClient):
  17. data_encoder = PickleDataEncoder
  18. my_python_object = {'hello': 'there'}
  19. gm_client = PickleExampleClient(['localhost:4730'])
  20. gm_client.submit_job("task_name", my_python_object)

Gearman Admin client

  1. class gearman.admin_client.GearmanAdminClient(host_list=None, poll_timeout=10.0)
  2. GearmanAdminClient :: Interface to send/receive administrative commands to a Gearman server
  3. This client acts as a BLOCKING client and each call will poll until it receives a satisfactory server response
  4. http://gearman.org/index.php?id=protocol See section ‘Administrative Protocol’

1 Interacting with a server

  1. GearmanAdminClient.send_maxqueue(task, max_size)
  2. # Sends a request to change the maximum queue size for a given task
  3. GearmanAdminClient.send_shutdown(graceful=True)
  4. # Sends a request to shutdown the connected gearman server
  5. GearmanAdminClient.get_status()
  6. # Retrieves a list of all registered tasks and reports how many items/workers are in the queue
  7. GearmanAdminClient.get_version()
  8. # Retrieves the version number of the Gearman server
  9. GearmanAdminClient.get_workers()
  10. # Retrieves a list of workers and reports what tasks they’re operating on
  1. # example
  2. Checking server state:
  3. gm_admin_client = gearman.GearmanAdminClient(['localhost:4730'])
  4. # Inspect server state
  5. status_response = gm_admin_client.get_status()
  6. version_response = gm_admin_client.get_version()
  7. workers_response = gm_admin_client.get_workers()

2 Testing server response times

  1. GearmanAdminClient.ping_server()
  2. # Sends off a debugging string to execute an application ping on the Gearman server
  1. # example
  2. Checking server response time:
  3. gm_admin_client = gearman.GearmanAdminClient(['localhost:4730'])
  4. response_time = gm_admin_client.ping_server()

Gearman job definitions

1 GearmanJob - Basic information about a requested job

  1. class gearman.job.GearmanJob(connection, handle, task, unique, data)
  2. # Represents the basics of a job... used in GearmanClient / GearmanWorker to represent job states

1.1 Server identifers

  1. GearmanJob.connection
  2. # GearmanConnection - Server assignment. Could be None prior to client job submission
  3. GearmanJob.handle
  4. # string - Job’s server handle. Handles are NOT interchangeable across different gearman servers

1.2 Job parameters

  1. GearmanJob.task
  2. # string - Job’s task
  3. GearmanJob.unique
  4. # string - Job’s unique identifier (client assigned)
  5. GearmanJob.data
  6. # binary - Job’s binary payload

2 GearmanJobRequest - State tracker for requested jobs

  1. class gearman.job.GearmanJobRequest(gearman_job, initial_priority=None, background=False, max_attempts=1)
  2. # Represents a job request... used in GearmanClient to represent job states

2.1 Tracking job submission

  1. GearmanJobRequest.gearman_job
  2. # GearmanJob - Job that is being tracked by this GearmanJobRequest object
  3. GearmanJobRequest.priority
  4. # PRIORITY_NONE [default]
  5. # PRIORITY_LOW
  6. # PRIORITY_HIGH
  7. GearmanJobRequest.background
  8. # boolean - Is this job backgrounded?
  9. GearmanJobRequest.connection_attempts
  10. # integer - Number of attempted connection attempts
  11. GearmanJobRequest.max_connection_attempts
  12. # integer - Maximum number of attempted connection attempts before raising an exception

2.2 Tracking job progress

  1. GearmanJobRequest.result
  2. # binary - Job’s returned binary payload - Populated if and only if JOB_COMPLETE
  3. GearmanJobRequest.exception
  4. # binary - Job’s exception binary payload
  5. GearmanJobRequest.state
  6. # JOB_UNKNOWN - Request state is currently unknown, either unsubmitted or connection failed
  7. # JOB_PENDING - Request has been submitted, pending handle
  8. # JOB_CREATED - Request has been accepted
  9. # JOB_FAILED - Request received an explicit job failure (job done but errored out)
  10. # JOB_COMPLETE - Request received an explicit job completion (job done with results)
  11. GearmanJobRequest.timed_out
  12. # boolean - Did the client hit its polling_timeout prior to a job finishing?
  13. GearmanJobRequest.complete
  14. # boolean - Does the client need to continue to poll for more updates from this job?

2.3 Tracking in-flight job updates

  1. Certain GearmanJobs may send back data prior to actually completing. GearmanClient uses these queues to keep track of what/when we received certain updates.
  2. GearmanJobRequest.warning_updates
  3. # collections.deque - Job’s warning binary payloads
  4. GearmanJobRequest.data_updates
  5. # collections.deque - Job’s data binary payloads
  6. GearmanJobRequest.status
  7. # dictionary - Job’s status
  8. handle - string - Job handle
  9. # known - boolean - Is the server aware of this request?
  10. # running - boolean - Is the request currently being processed by a worker?
  11. # numerator - integer
  12. # denominator - integer
  13. # time_received - integer - Time last updated
  14. New in version 2.0.1: Replaces GearmanJobRequest.status_updates and GearmanJobRquest.server_status
  15. GearmanJobRequest.status_updates
  16. # Deprecated since version 2.0.1: Replaced by GearmanJobRequest.status
  17. GearmanJobRequest.server_status
  18. # Deprecated since version 2.0.1: Replaced by GearmanJobRequest.status

python-gearman使用的更多相关文章

  1. gearman background后台job状态获取

    GearmanClient background job有一个方法叫: public array GearmanClient::jobStatus ( string $job_handle ) Get ...

  2. 分布式任务系统gearman的python实战

    Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在调用其它语言的函数的系统.Gearman是一个分发任务的程序框架,可以 ...

  3. 【python】gearman阻塞非阻塞,同步/异步,状态

    参考: http://pythonhosted.org/gearman/client.html?highlight=submit_multiple_jobs#gearman.client.Gearma ...

  4. 用 Gearman 分发 PHP 应用程序的工作负载

    尽管一个 Web 应用程序的大部分内容都与表示有关,但它的价值与竞争优势却可能体现在若干专有服务或算法方面.如果这类处理过于复杂或拖沓,最好是进行异步执行,以免 Web 服务器对传入的请求没有响应.实 ...

  5. 任务分发系统gearman

    1 Gearman是什么 Gearman Job Server@http://gearman.org/. Gearman 是一个任务分发系统,它提供了一个分发框架,能够分发某类任务到更适合处理这类任务 ...

  6. Gearman + Nodejs + MySQL UDF异步实现 MySQL 到 Redis 的数据同步

    [TOC] 1, 环境 CentOS, MySQL, Redis, Nodejs 2, Redis简介 Redis是一个开源的K-V内存数据库,它的key可以是string/set/hash/list ...

  7. gearman with postgresql as persistent Queuing

    gearman is a good thing gearman client --------------> gearman server <----------------------- ...

  8. 156个Python网络爬虫资源

    本列表包含Python网页抓取和数据处理相关的库. 网络相关 通用 urllib - 网络库(标准库) requests - 网络库 grab - 网络库(基于pycurl) pycurl - 网络库 ...

  9. gearman学习笔记1

    1.简介       gearman是一个分布式开发框架,适合处理一些必须处理但是不影响主流程的操作,比如保存日志.发送邮件.缩略图片等.最早是基于perl语言的,2008年发布的时候改为C++语言开 ...

  10. 【转】Python 爬虫的工具列表【预】

    这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...

随机推荐

  1. CGContext ----生成图片--image

    //获取图片 + (UIImage*) getImageWithColor:(UIColor*)color andHeight:(CGFloat)height { CGRect r= CGRectMa ...

  2. 实施MySQL ReplicationDriver支持读写分离

    MySQL 提供支持读写分离的驱动类: com.mysql.jdbc.ReplicationDriver 替代 com.mysql.jdbc.Driver 注意,所有参数主从统一: jdbc:mysq ...

  3. POJ1051 P,MTHBGWB

    题目来源:http://poj.org/problem?id=1051 题目大意: Morse密码里每个字母用长度不定的点和线来表示,一条信息中字母的编码之间用空隙隔开.下表为Morse密码的编码表: ...

  4. linux 安装 配置网络 备份 快照

    安装系统准备: 1.软件准备 vmware workstation14.vm14key.centos系统镜像 secureCRT http://sw.bos.baidu.com/sw-search-s ...

  5. Linux磁盘分区管理

    1.分区步骤          fdisk -l                                  查看系统中的磁盘 fdisk /dev/vdb                   ...

  6. C++: int int& int * int**的区别、联系和用途

    1.int; int是C++关键字,表示整型,其大小是32位有符号整型,表示的范围是-2,147,483,648 到 2,147,483,647:在声明和定义变量时使用,它表示的意思是所声明或所定义的 ...

  7. grunt 安装使用(一)

    grunt 依赖nodejs,所有在使用前确保你安装了nodejs,然后开始执行grunt命令. .安装node nodejs安装教程 安装完成后在命令行,执行命令: node  -v 出现版本信息, ...

  8. Android NDK开发 环境配置(一) 之多重CPU的兼容性

    今天我学习Android Studio当中的NDK,为什么要学习NDK呢,是因为领导给我提了一个BUG,这个BUG就是Android 多重CPU怎样兼容性,我现在先说一下,Android Studio ...

  9. Ordering Tasks UVA - 10305(拓扑排序)

    在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...

  10. python3+Appium自动化13-H5元素定位实践案例

    测试场景 启动钉钉app进入工作H5页面,点击考勤签到 查看webview上元素 1.电脑上打开chrome浏览器输入:chrome://inspect/#devices 2.Discover USB ...