这篇重点介绍C-Lib库及client和worker的开发,以0.14版libgearman for C来说解php
Client APIweb
client初始化&析构app
gearman_client_st *gearman_client_create(gearman_client_st *client)less
void gearman_client_free(gearman_client_st *client)dom
gearman_return_t gearman_client_add_server(gearman_client_st *client, const char *host, in_port_t port);异步
gearman_return_t gearman_client_add_servers(gearman_client_st *client, const char *servers); (一次添加多个gearman job-server)socket
同步操做ide
void * gearman_client_do(gearman_client_st *client, const char *function_name, const char *unique, const void *workload, size_t workload_size, size_t *result_size, gearman_return_t *ret_ptr);函数
其中:oop
i. unique: 的做用是client添加job给worker的时候的一个惟一标识,可选,默认是NULL
ii. workload & workload_size指代执行任务的详细参数及其大小
iii. result_size [out],指代返回数据的大小
iv. ret_ptr [out], 指代gearman的返回status,如下是官方对于返回status的一些说明:
In the case of GEARMAN_WORK_DATA, GEARMAN_WORK_WARNING, or GEARMAN_WORK_STATUS, the caller should take any actions to handle the event and then call this function again. This may happen multiple times until a GEARMAN_WORK_ERROR, GEARMAN_WORK_FAIL, or GEARMAN_SUCCESS (work complete) is returned. For GEARMAN_WORK_DATA or GEARMAN_WORK_WARNING, the result_size will be set to the intermediate data chunk being returned and an allocated data buffer will be returned. For GEARMAN_WORK_STATUS, the caller can use gearman_client_do_status() to get the current tasks status.
总而言之,只有GEARMAN_WORK_ERROR/GEARMAN_WORK_FAIL/GEARMAN_SUCCESS才是三个最终的返回结果,其余的只是临时中间结果,须要进一步调用接受结果的函数(感受中间结果只有在异步调用过程当中才会出现)
v. 输出是返回数据的起始地址,一旦用户用完以后,必须free,不然会出现内存泄露。
void gearman_set_timeout(gearman_universal_st *gearman, int timeout);
设置gearman_client_do多长时调用无返回则超时时间
异步callback操做
Gearman经过使用gearman_client_add_task()来望gearman_client_st中添加task,经过gearman_client_set_created_fn() / gearman_client_set_complete_fn()等来注册callback function,经过gearman_client_run_tasks()来运行gearman_client_st中的task。
异步background操做
系统在background运行job,client按期得到job运行结果,若是成功则返回,反之则继续等待。
gearman_return_t gearman_client_do_background(gearman_client_st *client, const char *function_name, const char *unique, const void *workload, size_t workload_size, char *job_handle);
i. job_handle [out]: 一个job的标识符
ii. 输出:返回状态
* gearman_return_t gearman_client_job_status(gearman_client_st *client, gearman_job_handle_t job_handle, bool *is_known, bool * is_running, uint32_t *numerator, uint32_t *denominator);
* 用户得到在background执行的job的状态
i. is_known [out]: Optional parameter to store the known status in
ii. is_running [out]: Optional parameter to store the running status in
iii. numerator [out]: Optional parameter to store the numerator in
iv. denominator [out]: Optional parameter to store the denominator in
PS: 好像background操做不怎么好使,不知道如何经过得到background的运行结果,这个是我一直困惑的
gearman_client_st的一些属性
gearman_client_st一共有如下3种运行属性:
i. GEARMAN_CLIENT_NON_BLOCKING: client运行在non-blocking mode
ii. GEARMAN_CLIENT_FREE_TASKS: 在task执行完成以后,自动的释放task
iii. GEARMAN_CLIENT_UNBUFFERED_RESULT: Allow the client to read data in chunks rather than have the library buffer the entire data result and pass that back。
能够经过函数gearman_client_add_options() / gearman_client_remove_options() / gearman_client_has_option() 等进行属性添加/删除/判断等
Worker API
/**
* Initialize a worker structure. Always check the return value even if passing
* in a pre-allocated structure. Some other initialization may have failed. It
* is not required to memset() a structure before providing it.
*
* @param[in] worker Caller allocated structure, or NULL to allocate one.
* @return On success, a pointer to the (possibly allocated) structure. On
* failure this will be NULL.
*/
GEARMAN_API
gearman_worker_st *gearman_worker_create(gearman_worker_st *worker);
/**
* Free resources used by a worker structure.
*
* @param[in] worker Structure previously initialized with
* gearman_worker_create() or gearman_worker_clone().
*/
GEARMAN_API
void gearman_worker_free(gearman_worker_st *worker);
/**
* Add a job server to a worker. This goes into a list of servers that can be
* used to run tasks. No socket I/O happens here, it is just added to a list.
*
* @param[in] worker Structure previously initialized with
* gearman_worker_create() or gearman_worker_clone().
* @param[in] host Hostname or IP address (IPv4 or IPv6) of the server to add.
* @param[in] port Port of the server to add.
* @return Standard gearman return value.
*/
GEARMAN_API
gearman_return_t gearman_worker_add_server(gearman_worker_st *worker,
const char *host, in_port_t port);
/**
* Add a list of job servers to a worker. The format for the server list is:
* SERVER[:PORT][,SERVER[:PORT]]...
* Some examples are:
* 10.0.0.1,10.0.0.2,10.0.0.3
* localhost LIBGEARMAN_BITFIELD234,jobserver2.domain.com:7003,10.0.0.3
*
* @param[in] worker Structure previously initialized with
* gearman_worker_create() or gearman_worker_clone().
* @param[in] servers Server list described above.
* @return Standard gearman return value.
*/
GEARMAN_API
gearman_return_t gearman_worker_add_servers(gearman_worker_st *worker,
const char *servers);
/**
* Register and add callback function for worker. To remove functions that have
* been added, call gearman_worker_unregister() or
* gearman_worker_unregister_all().
*
* @param[in] worker Structure previously initialized with
* gearman_worker_create() or gearman_worker_clone().
* @param[in] function_name Function name to register.
* @param[in] timeout Optional timeout (in seconds) that specifies the maximum
* time a job should. This is enforced on the job server. A value of 0 means
* an infinite time.
* @param[in] function Function to run when there is a job ready.
* @param[in] context Argument to pass into the callback function.
* @return Standard gearman return value.
*/
GEARMAN_API
gearman_return_t gearman_worker_add_function(gearman_worker_st *worker,
const char *function_name,
uint32_t timeout,
gearman_worker_fn *function,
void *context);
/**
* Wait for a job and call the appropriate callback function when it gets one.
*
* @param[in] worker Structure previously initialized with
* gearman_worker_create() or gearman_worker_clone().
* @return Standard gearman return value.
*/
GEARMAN_API
gearman_return_t gearman_worker_work(gearman_worker_st *worker);
/**
* See gearman_universal_set_timeout() for details.
*/
GEARMAN_API
void gearman_worker_set_timeout(gearman_worker_st *worker, int timeout);
开发实例
下面这个实例程序是,jfy_client发送test,jfy_worker返回test->result
下面的实例是PHP程序,客户端发送"hello!"
worker端是两个程序,一个是阻塞方式的,一个是非阻塞方式的