kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit d86a10afa10b7dc15f736f9780ec177269ff02b1
parent 2d380cac3f4707e28667030ae449710f28279831
Author: Joris Vink <joris@coders.se>
Date:   Wed, 22 Jan 2020 09:42:41 +0100

allow use udata in kore.timer() via the data kwarg.

Diffstat:
include/kore/python_methods.h | 5+++--
src/python.c | 19+++++++++++++++++--
2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/include/kore/python_methods.h b/include/kore/python_methods.h @@ -38,7 +38,6 @@ static PyObject *python_kore_log(PyObject *, PyObject *); static PyObject *python_kore_time(PyObject *, PyObject *); static PyObject *python_kore_lock(PyObject *, PyObject *); static PyObject *python_kore_proc(PyObject *, PyObject *); -static PyObject *python_kore_timer(PyObject *, PyObject *); static PyObject *python_kore_fatal(PyObject *, PyObject *); static PyObject *python_kore_queue(PyObject *, PyObject *); static PyObject *python_kore_worker(PyObject *, PyObject *); @@ -53,6 +52,7 @@ static PyObject *python_kore_task_kill(PyObject *, PyObject *); static PyObject *python_kore_prerequest(PyObject *, PyObject *); static PyObject *python_kore_task_create(PyObject *, PyObject *); static PyObject *python_kore_socket_wrap(PyObject *, PyObject *); +static PyObject *python_kore_timer(PyObject *, PyObject *, PyObject *); static PyObject *python_kore_domain(PyObject *, PyObject *, PyObject *); static PyObject *python_kore_gather(PyObject *, PyObject *, PyObject *); static PyObject *python_kore_sendobj(PyObject *, PyObject *, @@ -84,7 +84,6 @@ static struct PyMethodDef pykore_methods[] = { METHOD("time", python_kore_time, METH_NOARGS), METHOD("lock", python_kore_lock, METH_NOARGS), METHOD("proc", python_kore_proc, METH_VARARGS), - METHOD("timer", python_kore_timer, METH_VARARGS), METHOD("queue", python_kore_queue, METH_VARARGS), METHOD("worker", python_kore_worker, METH_VARARGS), METHOD("tracer", python_kore_tracer, METH_VARARGS), @@ -99,6 +98,7 @@ static struct PyMethodDef pykore_methods[] = { METHOD("prerequest", python_kore_prerequest, METH_VARARGS), METHOD("task_create", python_kore_task_create, METH_VARARGS), METHOD("socket_wrap", python_kore_socket_wrap, METH_VARARGS), + METHOD("timer", python_kore_timer, METH_VARARGS | METH_KEYWORDS), METHOD("server", python_kore_server, METH_VARARGS | METH_KEYWORDS), METHOD("gather", python_kore_gather, METH_VARARGS | METH_KEYWORDS), METHOD("domain", python_kore_domain, METH_VARARGS | METH_KEYWORDS), @@ -256,6 +256,7 @@ struct pytimer { int flags; struct kore_timer *run; PyObject *callable; + PyObject *udata; }; static PyObject *pytimer_close(struct pytimer *, PyObject *); diff --git a/src/python.c b/src/python.c @@ -2273,7 +2273,7 @@ python_kore_corotrace(PyObject *self, PyObject *args) } static PyObject * -python_kore_timer(PyObject *self, PyObject *args) +python_kore_timer(PyObject *self, PyObject *args, PyObject *kwargs) { u_int64_t ms; PyObject *obj; @@ -2291,6 +2291,7 @@ python_kore_timer(PyObject *self, PyObject *args) if ((timer = PyObject_New(struct pytimer, &pytimer_type)) == NULL) return (NULL); + timer->udata = NULL; timer->flags = flags; timer->callable = obj; timer->run = kore_timer_add(pytimer_run, ms, timer, flags); @@ -2298,6 +2299,13 @@ python_kore_timer(PyObject *self, PyObject *args) Py_INCREF((PyObject *)timer); Py_INCREF(timer->callable); + if (kwargs != NULL) { + if ((obj = PyDict_GetItemString(kwargs, "data")) != NULL) { + Py_INCREF(obj); + timer->udata = obj; + } + } + return ((PyObject *)timer); } @@ -2581,9 +2589,11 @@ pytimer_run(void *arg, u_int64_t now) struct pytimer *timer = arg; PyErr_Clear(); - ret = PyObject_CallObject(timer->callable, NULL); + ret = PyObject_CallFunctionObjArgs(timer->callable, timer->udata, NULL); Py_XDECREF(ret); + Py_XDECREF(timer->udata); + timer->udata = NULL; kore_python_log_error("pytimer_run"); if (timer->flags & KORE_TIMER_ONESHOT) { @@ -2621,6 +2631,11 @@ pytimer_close(struct pytimer *timer, PyObject *args) timer->callable = NULL; } + if (timer->udata != NULL) { + Py_DECREF(timer->udata); + timer->udata = NULL; + } + Py_INCREF((PyObject *)timer); Py_RETURN_TRUE; }