Pausing a program - poll/sleep/threads?

Simon John simoninusa2001 at yahoo.co.uk
Sat Feb 19 14:04:24 EST 2005


OK, I've implemented the 2sec threaded update, but I'm having some
problems with it.

Basically the thread will have to just run constantly, never exiting
(just sleeping for 2secs), which seems to work OK except when I try to
get the thread to do anything with the main program's window.

As the thread doesn't end, it doesn't return a result, so on every pass
of the thread's while loop, I write the current track info to the
mainWindow directly, however this causes the program to hang. Here is a
snippet of the current method:

class ThreadedInfo(Thread):
    """overloads init and defines run"""
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        # runs the whole time
        while 1:
            self.info()
            time.sleep(2)

    def info(self):
        """referencing window kills linux here"""

        # get track info, returned as dictionary
        self.info_dict = backend.getInfo(config.server, config.port)

I was thinking of getting the thread to just run once, then getting the
main program to write the result (using join() ) to the mainWindow.
That method would be something like this, but it's almost totally
pointless using a thread then, as the while loop will singletask the
program:

while 1:
    # start thread
    self.infothread.start()

    # when thread ends, get result
    self.result = self.infothread.join()

    # write result to window
    window.mainTextarea.setText(self.result)

    # pause for 2secs before starting again
    sleep(2)

I made sure that I built PyQt using threads, so that's not the issue.
Any other ideas?




More information about the Python-list mailing list