Skip to content

Commit

Permalink
Add check interrupts function.
Browse files Browse the repository at this point in the history
Previously, if a python function took a long time, there was no way to
interrupt it.
The check_interrupts function now exposes the CHECK_INTERRUPTS PostgreSQL macro
to trap signals.
  • Loading branch information
rdunklau committed Feb 22, 2016
1 parent 40e7648 commit 64ce596
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/multicorn/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from logging import ERROR, INFO, DEBUG, WARNING, CRITICAL
try:
from ._utils import _log_to_postgres
from ._utils import check_interrupts
except ImportError as e:
from warnings import warn
warn("Not executed in a postgresql server,"
Expand All @@ -24,3 +25,4 @@ def log_to_postgres(message, level=INFO, hint=None, detail=None):
if code is None:
raise KeyError("Not a valid log level")
_log_to_postgres(message, code, hint=hint, detail=detail)

11 changes: 11 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Python.h>
#include "postgres.h"
#include "multicorn.h"
#include "miscadmin.h"


struct module_state
Expand Down Expand Up @@ -114,8 +115,18 @@ log_to_postgres(PyObject *self, PyObject *args, PyObject *kwargs)
return Py_None;
}

static PyObject *
py_check_interrupts(PyObject *self, PyObject *args, PyObject *kwargs)
{
CHECK_FOR_INTERRUPTS();
Py_INCREF(Py_None);
return Py_None;
}


static PyMethodDef UtilsMethods[] = {
{"_log_to_postgres", (PyCFunction) log_to_postgres, METH_VARARGS | METH_KEYWORDS, "Log to postresql client"},
{"check_interrupts", (PyCFunction) py_check_interrupts, METH_VARARGS | METH_KEYWORDS, "Gives control back to PostgreSQL"},
{NULL, NULL, 0, NULL}
};

Expand Down

0 comments on commit 64ce596

Please sign in to comment.