[Stackless] Problems with removing tasklet.

Jeff Senn stackless-python at maya.com
Thu Oct 10 15:53:02 CEST 2002


Gustavo Niemeyer <niemeyer at conectiva.com> writes:

> Where's the code? Where's the code? :-)

Ok -- before we get to that -- read Christian's response carefully... :-)

> I've read that about tasklet switching many times. I'd like to
> understand better what kind of problems one can have when switching
> preemptively. I mean, in addition to the well-known problems which
> traditional parallel computing presents. I understand that variables
> returned from the stack can easily break, but could you mention
> something else?

Well... I'm not talking about the traditional problems of writing asynchronous code -- there are many of those.  

In this case there are some hard to understand details: for starters
(as Chris mentioned) not using any extension that might call back into
the interpreter.

Also the way the exception handling works you should be careful to catch
any exceptions *within* the tasklet -- and probably put the "main" tasklet
away (remove it?) while you are free scheduling everyone else...

(Of course if you deeply understand what is really happening you can
do the above...)

My patch to stacklessmodule.c is fairly trivial.  It is included below.
Don't blame me (or Christian) if it doesn't work :-) :-)

Oh ... I really don't know how much of stackless you understand -- Note:
this is still not *real* pre-emptive scheduling --- anything you do
that blocks the underlying OS native thread (i.e. blocking I/O) will
block all tasklets running in that thread...

Also note that it is not really very "fair" scheduling either -- you can
easily construct cases where tasklets are starved for cycles.  A fair
scheduler is much more work...

-- 
-Jas   --------------------     www.maya.com
       Jeff Senn          |   / / |-/ \ / /|®
       Chief Technologist |  /|/| |/ o | /-|
       Head of R&D        | Taming Complexity®

-------------snip-------------------

Index: stacklessmodule.c
===================================================================
RCS file: /home/cvs/stackless/src/Stackless/stacklessmodule.c,v
retrieving revision 1.77
diff -u -r1.77 stacklessmodule.c
--- stacklessmodule.c	2002/09/20 16:50:19	1.77
+++ stacklessmodule.c	2002/10/10 13:39:32
@@ -186,7 +186,53 @@
     return PyStackless_RunWatchdog(timeout);
 }
 
+/*++JAS */
+static char free_schedule__doc__[] =
+"free_schedule(interval) -- enable pre-emptive scheduling. Incompatible with"
+" run_watchdog.  Interval = 0 -- disables.";
 
+static int
+interrupt_schedule_next(void)
+{
+  PyThreadState *ts = PyThreadState_GET();
+  PyTaskletObject *task = ts->st.current;
+  PyTaskletObject *next = (PyTaskletObject*)task->next;
+
+  if (!slp_get_flags(task)->atomic && task != next && ts->st.runcount > 1 &&
+      ts->st.tmp.next->f.frame->f_tstate == ts) {
+    PyStackless_Schedule();
+  }
+  return 0;
+}
+
+static 
+PyObject *
+free_schedule(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    static char *argnames[] = {"interval", NULL};
+    long interval = 0;
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:free_schedule", argnames, &interval))
+      return NULL;
+
+#ifdef STACKLESS
+    {
+	PyThreadState *ts = PyThreadState_GET();
+        if(interval == 0) {
+	  ts->st.interrupt = 0;
+	} else {
+	  ts->st.interval = interval;
+	  ts->st.interrupt = interrupt_schedule_next;
+	}
+    }
+#endif
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+
+/*--JAS */
+
+
 static char test_cframe__doc__[] =
 "test_cframe(switches, words=0) -- a builtin testing function that does nothing\n\
 but tasklet switching. The function will call schedule() for switches\n\
@@ -431,6 +477,9 @@
     {"test_cframe",     (PyCFunction)test_cframe, METH_VARARGS | METH_KEYWORDS, test_cframe__doc__},
     {"set_channel_callback", (PyCFunction)set_channel_callback, METH_O, set_channel_callback__doc__},
     {"set_schedule_callback", (PyCFunction)set_schedule_callback, METH_O, set_schedule_callback__doc__},
+    /*++JAS*/
+    {"free_schedule",	(PyCFunction)free_schedule, METH_KEYWORDS, free_schedule__doc__},
+    /*--JAS*/
 #ifdef STACKLESS_SPY
     {"_peek", (PyCFunction)_peek, METH_O, _peek__doc__},
 #endif

_______________________________________________
Stackless mailing list
Stackless at www.tismer.com
http://www.tismer.com/mailman/listinfo/stackless



More information about the Stackless mailing list