Win98 threads

Peter Hansen peter at engcorp.com
Mon Dec 16 08:48:33 EST 2002


Anand B Pillai wrote:
> 
> Does pyhton not support the 'threading' module in Windows 98. I find that
> the Thread  class in the threading module does not work very well on Windows 98.
> Most of the time it does not call the thread target.

Please post a short example that demonstrates the problem.  These things
do work, and very reliably.

> Also os.name returns 'nt' on Win 98. How can I identify the correct OS
> name if I need to take care of it in my program (to tell it not to use the
> Thread class on Win 98 for example).

Generally this is a bad thing.  You want to test for functionality,
not specific versions.  That is, test whether the thing you want is
present, and catch an exception if it is not.  Of course, this might
be hard in a case like you are describing, but luckily you will *not*
need to do it in this specific case.

Try this from the prompt if you are having trouble (warning, untested
code, but I type this stuff frequently so it "ought" to work):

import threading
class Task(threading.Thread):
    def terminate(self):
        self._terminate = 1

    def run(self):
        print 'starting'
        import time
        self._terminate = 0
        while not self._terminate:
            print 'working'
            time.sleep(5)
        print 'stopping'

>>> t = Task()
>>> t.start()
starting
>>> # now wait a few seconds to let the thread print its stuff
working
working
working
>>> # when you're done, do this to finish cleanly:
>>> t.terminate()
stopping

-Peter



More information about the Python-list mailing list