in Education by
I have simple PyGTK app. Since I have to run multiple periodic tasks to fetch some data and refresh GUI, I extended Thread like this: class MyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.setDaemon(True) self.event = threading.Event() self.event.set() def run(self): while self.event.is_set(): timer = threading.Timer(60, self._run) timer.start() timer.join() def cancel(self): self.event.clear() def _run(self): gtk.threads_enter() # do what need to be done, fetch data, update GUI gtk.threads_leave() I start threads on app bootstrap, save them in some list and cancel them before exit. This works just perfect. But now I want to add refresh button which will force one of the threads to run immediately and not wait period of time to be run, if not currently running. I tried to do that by adding bool var to MyThread to indicate whether a thread is running or not (set before _run, reset on complete), and then just call MyThread._run() if not running, but that causes my app to become unresponsive and _run task to never finish execution. I'm not sure why this happens. What is the best way to solve this problem? It would be also fine if I can make refresh running in background so it does not block GUI. Maybe to call run and pass in number of seconds to 1 so timer can trigger it sooner? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Instead of using a Timer, use another Event object in combination with a timeout. You can then set that event from within your button callback. The following code illustrates this (I've stripped your cancelling code to keep it short): import threading class MyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.sleep_event = threading.Event() self.damon = True def run(self): while True: self.sleep_event.clear() self.sleep_event.wait(60) threading.Thread(target=self._run).start() def _run(self): print "run" my_thread = MyThread() my_thread.start() while True: raw_input("Hit ENTER to force execution\n") my_thread.sleep_event.set() By default "run" will be printed every 60 seconds. If you hit ENTER it will be printed immediately, and then again after 60 seconds, etc.

Related questions

0 votes
    I use the on-demand (hosted) version of FogBugz. I would like to start using Mercurial for source ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    What is group displacement law ? An element belonging to group 1 decay by β c − − β emission. To which ... element will belong ? Select the correct answer from above options...
asked Jan 2, 2022 in Education by JackTerrance
0 votes
    If a function f(t) is periodic, then? (a) f(t) = f(t+T) (b) f(t) = f(t+T/2) (c) ... , Network theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 20, 2021 in Education by JackTerrance
0 votes
    In the figure given below, what is the RMS value of the periodic waveform? (a) 2\sqrt{6} A (b) 6\ ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 17, 2021 in Education by JackTerrance
0 votes
    A periodic rectangular signal X (t) has the waveform as shown below. The frequency of the fifth harmonic of its ... GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 16, 2021 in Education by JackTerrance
0 votes
    A periodic voltage v (t) = 1 + 4 sin ωt + 2 cos ωt is applied across a 1Ω resistance. The power ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    In _________ systems, resources are allocated on demand. A. packet switching B. circuit switching C. line switching D. frequency switching...
asked Jan 6, 2023 in Education by JackTerrance
0 votes
    Amazon's EC2 service offers a variety of Linux and Windows OS choices, but I haven't found a service ... a monthly subscription. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    Optical disk _______ systems contain a few drives and numerous disks that can be loaded into one of the ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Tuples are generated ___________ in producer-driven pipelining, they are generated ________ on demand, in demand ... in division Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    What is on-demand functionality? How is it provided in cloud computing?...
asked Dec 15, 2020 in Technology by JackTerrance
0 votes
    Develop on Cadence and Release on Demand is part of which SAFe Core Competency?...
asked Nov 27, 2020 in Technology by JackTerrance
0 votes
    Is there any difference between the terms Data on Demand & Details on Demand?...
asked Nov 21, 2020 in Technology by JackTerrance
0 votes
    I built an OCR application which reads PDF files and OCR's them. I built it using Multi-threading ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
...