Pause a thread/ execfile()

B

Babloo

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
 
S

Sean DiZazzo

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()
 
S

Saju Pillai

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 .

Other ideas ? You could use job control signals if you are on unix. Try
forking a child process instead of using a thread. Sending SIGSTOP to
the forked child will stop/pause it, SIGCONT will resume it.

-srp
 
B

Babloo

Other ideas ? You could use job control signals if you are on unix. Try
forking a child process instead of using a thread. Sending SIGSTOP to
the forked child will stop/pause it, SIGCONT will resume it.

-srp

Hi ,
Thanks for the reply . I am on windows platform ... yeah if was on
linux things would have been easier !!
 
B

Babloo

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()

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 
S

Sean DiZazzo

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
 
T

Terry Reedy

Babloo said:
Any ideas how to pause execfile()?

As far as the calling instance of the Python interpreter is concerned,
calling execfile (or any C function) is an atomic action. You need to
rewrite the code in the file executed to have it monitor a semaphore.
 
A

Aahz

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 .

You might be able to get something useful out of Python's debugger hooks
(create some kind of callback).
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"You could make Eskimos emigrate to the Sahara by vigorously arguing --
at hundreds of screens' length -- for the wonder, beauty, and utility of
snow." --PNH to rb in r.a.sf.f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top