allow coroutines to set friendly names.

This commit is contained in:
Joris Vink 2019-09-16 20:31:25 +02:00
parent 960fabe44c
commit 019006620d
2 changed files with 33 additions and 2 deletions

View File

@ -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),

View File

@ -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
@ -1517,6 +1525,26 @@ python_kore_shutdown(PyObject *self, PyObject *args)
Py_RETURN_TRUE;
}
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)
{