gearman_execute_example.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Example code to show how to send a string to a function called "reverse" and print the results.
  3. */
  4. /*
  5. # Gearman server and library
  6. # Copyright (C) 2012 Data Differential, http://datadifferential.com/
  7. # All rights reserved.
  8. #
  9. # Use and distribution licensed under the BSD license. See
  10. # the COPYING file in this directory for full text.
  11. */
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <libgearman/gearman.h>
  16. int main(void)
  17. {
  18. gearman_client_st *client= gearman_client_create(NULL);
  19. gearman_return_t ret= gearman_client_add_server(client, "localhost", 0);
  20. if (gearman_failed(ret))
  21. {
  22. return EXIT_FAILURE;
  23. }
  24. gearman_argument_t value= gearman_argument_make(0, 0, "Reverse Me", strlen("Reverse Me"));
  25. gearman_task_st *task= gearman_execute(client,
  26. "reverse", strlen("reverse"), // function
  27. NULL, 0, // no unique value provided
  28. NULL,
  29. &value, 0);
  30. if (task == NULL) // If gearman_execute() can return NULL on error
  31. {
  32. fprintf(stderr, "Error: %s\n", gearman_client_error(client));
  33. gearman_client_free(client);
  34. return EXIT_FAILURE;
  35. }
  36. // Make sure the task was run successfully
  37. if (gearman_success(gearman_task_return(task)))
  38. {
  39. // Make use of value
  40. gearman_result_st *result= gearman_task_result(task);
  41. printf("%.*s\n", (int)gearman_result_size(result), gearman_result_value(result));
  42. }
  43. gearman_client_free(client);
  44. return EXIT_SUCCESS;
  45. }