1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* gearman-interface: Interface Wrappers for Gearman
5
* Copyright (C) 2009 Sun Microsystems, Inc.
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.
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.
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
22
%typemap(in) gearman_callback *worker_fn {
24
if (!PyCallable_Check($input)) {
25
PyErr_SetString(PyExc_TypeError, "Need a callable object!");
28
$1= (gearman_callback *)PyMem_Malloc(sizeof(gearman_callback));
29
$1->callback_obj= $input;
37
/* TODO: I know this looks silly - it's cause we're going to add the job to it */
39
typedef struct py_gearman_callback_st {
40
PyObject *callback_obj;
44
void* workerCallback(gearman_job_st *job, void *fn_arg,
46
gearman_return_t *ret_ptr)
48
gearman_callback *cb= (gearman_callback *)fn_arg;
49
PyObject *func = cb->callback_obj; // Get Python callable
51
PyObject *job_obj = PyBuffer_FromMemory((void *)gearman_job_workload(job),
52
gearman_job_workload_size(job));
54
PyObject *arglist = Py_BuildValue("(O)", job_obj);
56
PyObject *result= PyEval_CallObject(func, arglist);
57
PyObject *py_error= PyErr_Occurred();
64
*ret_ptr= GEARMAN_WORK_EXCEPTION;
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
75
void *result_bytes= malloc(*result_size);
76
memcpy(result_bytes, py_result_str, *result_size);