spammify.c: Convert spam.system to spam.busy for a GIL-less wait
authorW. Trevor King <wking@tremily.us>
Wed, 17 Apr 2013 15:58:21 +0000 (11:58 -0400)
committerW. Trevor King <wking@tremily.us>
Wed, 17 Apr 2013 15:58:21 +0000 (11:58 -0400)
I want to see if GIL-dropping commands will use several cores.

spammify.c

index 31d7754da25e2d27a24978bb71e8ee0c0a2dc731..1001f341be1c44635dfaf6c273b7eb5b52321d9c 100644 (file)
@@ -1,21 +1,27 @@
 #include <Python.h>
 #include <stdlib.h>
+#include <time.h>
 
 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 */
 };