[Pythonmac-SIG] Cooperative threading on Mac (was: playing well with others (newbie question))

Jack Jansen jack@oratrix.nl
Fri, 31 Dec 1999 16:50:16 +0100


Recently, Skip Montanaro <skip@mojam.com> said:
> 
> (I've redirected this from the main list to the PythonMac-SIG.)
> 
>     Michael> I'm stuck with the following problem with python 1.5.2c1 on
>     Michael> MacOS 8.6: 
> 
>     Michael> while 1:
>     Michael>     pass
> 
>     Michael> grinds my system to a halt. In other languages there are
>     Michael> commands that give away cycles to the rest of the system and
>     Michael> thus "playing well with others." Is there some similar command
>     Michael> in python? Something to replace that pass with that allows me
>     Michael> to do other things with my system?
> 
> The code that implements pass in ceval.c could easily be modified to
> periodically call whatever Mac toolbox function is used to release the
> processor.

That code is indeed in place (unless you specifically disable it by
calling MacOS.SchedParams() with various arguments to turn it off).
You can also use SchedParams() to change the actual allocation of
cycles to others when Python is in foreground or background (default
is pretty greedy in the foreground, not greedy in the background).

There does seem to be a bug in the current release, though, that I
haven't seen before: when you switch to a background application this
causes Python to monopolize the CPU for 15 seconds (not even the clock 
runs). After that everything is as sketched above. I will investigate
what is going on.

>  Unfortunately, the Mac-specific source code seems not to be
> under CVS control, so while I can see some tantalizing hints of what needs
> doing in the source on my Linux machine, I can't suggest anything specific.

MacPython is under CVS, see the homepage
(http://www.cwi.nl/~jack/macpython.html) for details.
--
Jack Jansen             | ++++ stop the execution of Mumia Abu-Jamal ++++
Jack.Jansen@oratrix.com | ++++ if you agree copy these lines to your sig ++++
www.oratrix.nl/~jack    | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm