From 946abfdead1b03000ff3d494570ec4ca3bdda8c2 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 17 Apr 2013 11:58:21 -0400 Subject: [PATCH] spammify.c: Convert spam.system to spam.busy for a GIL-less wait I want to see if GIL-dropping commands will use several cores. --- spammify.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/spammify.c b/spammify.c index 31d7754..1001f34 100644 --- a/spammify.c +++ b/spammify.c @@ -1,21 +1,27 @@ #include #include +#include static PyObject * -spam_system(PyObject *self, PyObject *args) +spam_busy(PyObject *self, PyObject *args) { - const char *command; - int sts; + int seconds; + time_t start; + long int i = 0; - if (!PyArg_ParseTuple(args, "s", &command)) + if (!PyArg_ParseTuple(args, "i", &seconds)) return NULL; - sts = system(command); - return PyLong_FromLong(sts); + Py_BEGIN_ALLOW_THREADS + start = time(NULL); + while (time(NULL) < start + seconds) + i++; + Py_END_ALLOW_THREADS + return PyLong_FromLong(i); } static PyMethodDef SpamMethods[] = { - {"system", spam_system, METH_VARARGS, - "Execute a shell command."}, + {"busy", spam_busy, METH_VARARGS, + "Drop the GIL and run a busy wait for a few seconds."}, {NULL, NULL, 0, NULL} /* Sentinel */ }; -- 2.26.2