~gearman-developers/gearman-interface/gearman-interface

« back to all changes in this revision

Viewing changes to interface/python/worker_callback.i

  • Committer: Monty Taylor
  • Date: 2009-07-21 20:41:39 UTC
  • Revision ID: mordred@inaugust.com-20090721204139-rcyse999id1lbu1h
Python worker initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  gearman-interface: Interface Wrappers for Gearman
 
5
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 */
 
21
 
 
22
%typemap(in) gearman_callback *worker_fn {
 
23
 
 
24
  if (!PyCallable_Check($input)) {
 
25
    PyErr_SetString(PyExc_TypeError, "Need a callable object!");
 
26
    return NULL;
 
27
  }
 
28
  $1= (gearman_callback *)PyMem_Malloc(sizeof(gearman_callback));
 
29
  $1->callback_obj= $input;
 
30
  Py_INCREF($input);
 
31
 
 
32
}
 
33
 
 
34
 
 
35
%{
 
36
 
 
37
/* TODO: I know this looks silly - it's cause we're going to add the job to it */
 
38
 
 
39
  typedef struct py_gearman_callback_st {
 
40
    PyObject *callback_obj;
 
41
  } gearman_callback;
 
42
 
 
43
 
 
44
  void* workerCallback(gearman_job_st *job, void *fn_arg,
 
45
                       size_t *result_size,
 
46
                       gearman_return_t *ret_ptr)
 
47
  {
 
48
    gearman_callback *cb= (gearman_callback *)fn_arg;
 
49
    PyObject *func = cb->callback_obj;   // Get Python callable
 
50
    PyMem_Free(fn_arg);
 
51
    PyObject *job_obj = PyBuffer_FromMemory((void *)gearman_job_workload(job),
 
52
                                            gearman_job_workload_size(job));
 
53
 
 
54
    PyObject *arglist = Py_BuildValue("(O)", job_obj);
 
55
 
 
56
    PyObject *result= PyEval_CallObject(func, arglist);
 
57
    PyObject *py_error= PyErr_Occurred();
 
58
 
 
59
    Py_DECREF(func);
 
60
    Py_DECREF(arglist);
 
61
 
 
62
    if (py_error != NULL)
 
63
    {
 
64
      *ret_ptr= GEARMAN_WORK_EXCEPTION;
 
65
      *result_size= NULL;
 
66
      PyErr_Clear();
 
67
      return NULL;
 
68
    }
 
69
    *ret_ptr= GEARMAN_SUCCESS;
 
70
    char *py_result_str= PyString_AsString(result);
 
71
    *result_size= (size_t)PyString_Size(result);
 
72
    /* TODO: Make this use PyMem_Malloc - talk to eric about
 
73
     * how the malloc args work
 
74
     */
 
75
    void *result_bytes= malloc(*result_size); 
 
76
    memcpy(result_bytes, py_result_str, *result_size);
 
77
    return result_bytes;
 
78
    
 
79
  }
 
80
%}
 
81