gearman_execute_example.c 1.3 KB

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