kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 019006620dde61e55bb3f51122cd3550ebffb1a6
parent 960fabe44c097fd71674371054bd9b1c27f191e0
Author: Joris Vink <joris@coders.se>
Date:   Mon, 16 Sep 2019 20:31:25 +0200

allow coroutines to set friendly names.

Diffstat:
include/kore/python_methods.h | 3+++
src/python.c | 32++++++++++++++++++++++++++++++--
2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/include/kore/python_methods.h b/include/kore/python_methods.h @@ -21,6 +21,7 @@ struct python_coro { u_int64_t id; int state; PyObject *obj; + char *name; PyObject *result; struct pysocket_op *sockop; struct pygather_op *gatherop; @@ -46,6 +47,7 @@ static PyObject *python_kore_fatalx(PyObject *, PyObject *); static PyObject *python_kore_setname(PyObject *, PyObject *); static PyObject *python_kore_suspend(PyObject *, PyObject *); static PyObject *python_kore_shutdown(PyObject *, PyObject *); +static PyObject *python_kore_coroname(PyObject *, PyObject *); static PyObject *python_kore_bind_unix(PyObject *, PyObject *); static PyObject *python_kore_prerequest(PyObject *, PyObject *); static PyObject *python_kore_task_create(PyObject *, PyObject *); @@ -86,6 +88,7 @@ static struct PyMethodDef pykore_methods[] = { METHOD("setname", python_kore_setname, METH_VARARGS), METHOD("suspend", python_kore_suspend, METH_VARARGS), METHOD("shutdown", python_kore_shutdown, METH_NOARGS), + METHOD("coroname", python_kore_coroname, METH_VARARGS), METHOD("bind_unix", python_kore_bind_unix, METH_VARARGS), METHOD("prerequest", python_kore_prerequest, METH_VARARGS), METHOD("task_create", python_kore_task_create, METH_VARARGS), diff --git a/src/python.c b/src/python.c @@ -333,7 +333,9 @@ kore_python_coro_delete(void *obj) else TAILQ_REMOVE(&coro_suspended, coro, list); + kore_free(coro->name); Py_XDECREF(coro->result); + kore_pool_put(&coro_pool, coro); } @@ -553,6 +555,7 @@ python_coro_create(PyObject *obj, struct http_request *req) coro = kore_pool_get(&coro_pool); coro_count++; + coro->name = NULL; coro->result = NULL; coro->sockop = NULL; coro->gatherop = NULL; @@ -685,8 +688,13 @@ python_coro_trace(const char *label, struct python_coro *coro) else line = -1; - kore_log(LOG_NOTICE, "coro %" PRIu64 " %s <%s> @ [%s:%d]", - coro->id, label, func, fname, line); + if (coro->name) { + kore_log(LOG_NOTICE, "coro '%s' %s <%s> @ [%s:%d]", + coro->name, label, func, fname, line); + } else { + kore_log(LOG_NOTICE, "coro %" PRIu64 " %s <%s> @ [%s:%d]", + coro->id, label, func, fname, line); + } } #endif @@ -1518,6 +1526,26 @@ python_kore_shutdown(PyObject *self, PyObject *args) } static PyObject * +python_kore_coroname(PyObject *self, PyObject *args) +{ + const char *name; + + if (coro_running == NULL) { + PyErr_SetString(PyExc_RuntimeError, + "kore.coroname() only available in coroutines"); + return (NULL); + } + + if (!PyArg_ParseTuple(args, "s", &name)) + return (NULL); + + kore_free(coro_running->name); + coro_running->name = kore_strdup(name); + + Py_RETURN_NONE; +} + +static PyObject * python_kore_timer(PyObject *self, PyObject *args) { u_int64_t ms;