Pause a thread/ execfile()

Sean DiZazzo half.italian at gmail.com
Mon Oct 26 17:16:56 CET 2009


On Oct 26, 1:25 am, Babloo <pruthviraj... at gmail.com> wrote:
> On Oct 26, 1:01 pm, Sean DiZazzo <half.ital... at gmail.com> wrote:
>
>
>
> > On Oct 25, 11:58 pm, Babloo <pruthviraj... at gmail.com> 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()
>
> Hi..
> 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 threading.thread.run()
>         main control loop
>         """
>         print "%s starts" % (self.getName(),)
>
>         count = 0
>         while not self._stopevent.isSet():
>             count += 1
>             print "loop %d" % (count,)
>             self._stopevent.wait(self._sleepperiod)
>
>         print "%s ends" % (self.getName(),)
>
>     def join(self,timeout=None):
>         """
>         Stop the thread
>         """
>         self._stopevent.set()
>         threading.Thread.join(self, timeout)
>
> if __name__ == "__main__":
>     testthread = TestThread()
>     testthread.start()
>
>     import time
>     time.sleep(10.0)
>
>     testthread.join()
>
> $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

I was thinking along the lines of this.  I'll bet you can find a more
efficient way of doing it, but this demonstrates what I was thinking.

import threading, time

class TestThread(threading.Thread):
    def __init__(self):
        self._stopevent = threading.Event()
        threading.Thread.__init__(self)


    def run(self):
        count = 0
        while 1:
            if not self._stopevent.isSet():
                count += 1
                print count
                time.sleep(1)
            else:
                print "paused"
                time.sleep(1)

    def pause(self):
        self._stopevent.set()

    def play(self):
        self._stopevent.clear()


if __name__ == "__main__":
    th = TestThread()
    th.start()

    time.sleep(5)
    th.pause()

    time.sleep(5)
    th.play()

~Sean



More information about the Python-list mailing list