[Tutor] [tutor] Question on multithreading
bob gailer
bgailer at alum.rpi.edu
Wed Jan 30 05:41:22 CET 2008
Varsha Purohit wrote:
> Hello friends,
> I hve a GUI where i have start button and stop button. When i
> press start button one thread is created and it is executing some
> background task and when i press stop button that thread is
> stopped/killed. These two things are working properly. But i have to
> implement pause button in my GUI. When i press pause the thread
> execution should come to a halt and after that it should be resumed
> when i press pause again or may be i can implement some other button.
>
> Does anybody have an example where they have implemented this kind of
> threading concept ??
Take a look at the threading module. The thread should acquire() a
condition then wait(). The following is quickly thrown together and not
tested.
main program:
import threading
cond = threading.Condition() # create a condition variable with a new RLock
thread:
while True: # I assume the thread runs in some kind of loop
if pause: # pressing Pause should set this variable True
cond .acquire()
cond .wait() # pauses until notified
cond .release()
# rest of thread - assumes loop repeats fast enough to give the
desired response time
code behind the pause/resume button:
pause = not pause # toggle
if not pause:
cond.notify()
--
Bob Gailer
919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list