gearman_execute_partition.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Example code to show how to send a string to a function called "word_split", reduce
  3. each chunk with the function count, and then print the results.
  4. */
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <libgearman/gearman.h>
  9. int main(void)
  10. {
  11. gearman_client_st *client= gearman_client_create(NULL);
  12. gearman_return_t ret= gearman_client_add_server(client, "localhost", 0);
  13. if (gearman_failed(ret))
  14. {
  15. return EXIT_FAILURE;
  16. }
  17. gearman_argument_t value= gearman_argument_make(0, 0, "this dog does not hunt", strlen("this dog does not hunt"));
  18. gearman_task_st *task= gearman_execute_by_partition(client,
  19. "word_split", strlen("word_split"),
  20. "count", strlen("count"), // function
  21. NULL, 0, // no unique value provided
  22. NULL,
  23. &value, 0);
  24. if (task == NULL) // If gearman_execute() can return NULL on error
  25. {
  26. fprintf(stderr, "Error: %s\n", gearman_client_error(client));
  27. gearman_client_free(client);
  28. return EXIT_FAILURE;
  29. }
  30. // Make sure the task was run successfully
  31. if (gearman_success(gearman_task_return(task)))
  32. {
  33. // Make use of value
  34. gearman_result_st *result= gearman_task_result(task);
  35. printf("%.*s\n", (int)gearman_result_size(result), gearman_result_value(result));
  36. }
  37. gearman_client_free(client);
  38. return EXIT_SUCCESS;
  39. }