NewBie Doubt in Python Thread Programming
vijay swaminathan
swavijay at gmail.com
Wed May 11 05:08:55 EDT 2011
Sorry. My intention was not to send out a private message. when I chose
reply to all, I was confused if this would start as a new thread. so just
did a reply..
coming back,
I have developed a GUI based on pyQT4 which has a run button. when I click
on run, it invokes a command prompt and runs a .bat file.
Till the execution of .bat file is over, I want the run button on the GUI to
be disabled. so i thought of invoking the command prompt and running the
.bat file on a thread so that I could monitor the status of the thread
(assuming that the thread would be active till command prompt is active -
correct me if I'm wrong).
for this, the code that I had written is;
# to invoke a command prompt and execute the .bat file.
class RunMonitor(threading.Thread):
def __init__(self, parent=None):
threading.Thread.__init__(self)
def run(self):
print 'Invoking Command Prompt..........'
subprocess.call(["start", "/DC:\\Script", "scripts_to_execute.bat"],
shell=True)
A timer function to call this thread and monitor this thread..
def runscript(self):
self.timer = QTimer()
self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData)
self.timer.start(1000)
def sendData(self):
if self.run_timer:
run_monitor_object = RunMonitor()
print 'Starting the thread...........'
run_monitor_object.start()
self.run_timer = False
if run_monitor_object.isAlive():
print 'Thread Alive...'
else:
print 'Thread is Dead....'
Any flaw in the logic? any other better ways of achieving this?
On Wed, May 11, 2011 at 2:16 PM, Chris Angelico <rosuav at gmail.com> wrote:
> I'm responding to this on-list on the assumption that this wasn't
> meant to be private; apologies if you didn't intend for this to be the
> case!
>
> On Wed, May 11, 2011 at 6:38 PM, vijay swaminathan <swavijay at gmail.com>
> wrote:
> > so If i understand correctly, once the run method of the thread is
> executed,
> > the thread is no more alive.
>
> Once run() finishes executing, the thread dies.
>
> > Actually, I'm trying to invoke a command prompt to run some script and as
> > long as the script runs on the command prompt, I would like to have the
> > thread alive. But according to your statement, the thread would die off
> > after invoking the command prompt. is there a way to keep the thread
> active
> > till I manually close the command prompt?
>
> That depends on how the "invoke command prompt" function works.
>
> > A snippet of the code written is:
> > # Thread definition
> > class RunMonitor(QThread):
> > def __init__(self, parent=None):
> > QThread.__init__(self)
> > def run(self):
> > print 'Invoking Command Prompt..........'
> > subprocess.call(["start", "/DC:\\Scripts",
> > "scripts_to_execute.bat"], shell=True)
> >
> > def sendData(self):
> >
> > if self.run_timer:
> > run_monitor_object = RunMonitor()
> > print 'Starting the thread...........'
> > run_monitor_object.start()
> > self.run_timer = False
> >
> > if run_monitor_object.isAlive():
> > print 'Thread Alive...'
> > else:
> > print 'Thread is Dead....'
> >
>
> subprocess.call() will return immediately, so this won't work. But if
> you use os.system() instead, then it should do as you intend.
>
> > to check the status of the thread repeatedly I have the QTimer which
> would
> > call the self.sendData() for every minute.
> >
> > self.timer = QTimer()
> > self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData)
> > self.timer.start(1000)
>
> I'm not really sure what your overall goal is. Can you explain more of
> your high-level intentions for this program? There may be a much
> easier way to accomplish it.
>
> Chris Angelico
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Vijay Swaminathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110511/ae9883b9/attachment-0001.html>
More information about the Python-list
mailing list