Pause a thread/ execfile()

Babloo pruthviraj.pb at
Mon Oct 26 09:25:14 CET 2009

On Oct 26, 1:01 pm, Sean DiZazzo <half.ital... at> wrote:
> On Oct 25, 11:58 pm, Babloo <pruthviraj... at> wrote:
> > i have a small python application with GUI (frontend) which has
> > various functions. I have a "RUN" button which runs python scripts in
> > the background . It basically calls execfile() function internally
> > which runs in a thread ,  to run the python script .
> > I want to implement a "PAUSE" feature which would pause the running
> > python script . So do that i have to either pause the thread in which
> > execfile() runs or pause execfile itself .
> > When the user presses "RUN" again then the python script should resume
> > running .
> > Any ideas how to pause the thread / pause execfile() . Any other ideas
> > for the same would be helpful .
> > Thanks
> I think you can do that with a threading.event().  In the gui, create
> a new threading.event() and pass it in to the worker thread.  Set up
> your code in the worker so that on every iteration of "work", it
> checks to see if the event is set.  If it finds it set, then it
> sleeps, but keeps checking until the event is unset.  When unset
> again, it picks up and begins work again.
> In the gui, your pause button just sets and unsets the event, and the
> worker will find out and pause at the next iteration.
> Depending on what kind of work your worker thread is doing, it might
> be tough to structure the code so the event gets checked reasonably
> often.  Hope this helps.
> ~Sean
> PS.  Not sure this idea will hold up when using execfile()

Thanks for the reply . Yes tried doing exactly what you have
suggested . i could show you the sample code .
But some how it doesn't work and main reason could be i am using
execfile() in the thread .
I was trying to set the event on the press of a button . It doesnt
pause the thread and instead keeps on executing .
Dont know what the problem could be ???

Sample code :-

import threading

class TestThread(threading.Thread):
    A sample thread class

    def __init__(self):
        Constructor, setting initial variables
        self._stopevent = threading.Event()
        self._sleepperiod = 1.0

        threading.Thread.__init__(self, name="TestThread")

    def run(self):
        overload of
        main control loop
        print "%s starts" % (self.getName(),)

        count = 0
        while not self._stopevent.isSet():
            count += 1
            print "loop %d" % (count,)

        print "%s ends" % (self.getName(),)

    def join(self,timeout=None):
        Stop the thread
        threading.Thread.join(self, timeout)

if __name__ == "__main__":
    testthread = TestThread()

    import time



More information about the Python-list mailing list