這篇重點介紹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端是兩個程序,一個是阻塞方式的,一個是非阻塞方式的