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..<br><br>coming back,<br><br>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.<br>
<br>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).<br>
<br>for this, the code that I had written is;<br><br># to invoke a command prompt and execute the .bat file.<br>class RunMonitor(threading.Thread):<br>    def __init__(self, parent=None):<br>        threading.Thread.__init__(self)<br>
    def run(self):<br>        print 'Invoking Command Prompt..........'<br>        subprocess.call(["start", "/DC:\\Script", "scripts_to_execute.bat"], shell=True)<br><br><br>A timer function to call this thread and monitor this thread..<br>
<br><pre class="default prettyprint"><code><span class="pln"> </span><span class="kwd">def</span><span class="pln"> runscript</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">):</span><span class="pln"><br>
    </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">timer </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QTimer</span><span class="pun">()</span><span class="pln"><br>
    </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">timer</span><span class="pun">.</span><span class="pln">connect</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">timer</span><span class="pun">,</span><span class="pln"> SIGNAL</span><span class="pun">(</span><span class="str">"timeout()"</span><span class="pun">),</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">sendData</span><span class="pun">)</span><span class="pln"><br>
    </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">timer</span><span class="pun">.</span><span class="pln">start</span><span class="pun">(</span><span class="lit">1000</span><span class="pun">)</span><span class="pln"> <br>
<br></span><span class="kwd">def</span><span class="pln"> sendData</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">):</span><span class="pln"><br><br><br>    </span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">run_timer</span><span class="pun">:</span><span class="pln"><br>
        run_monitor_object </span><span class="pun">=</span><span class="pln"> </span><span class="typ">RunMonitor</span><span class="pun">()</span><span class="pln"><br>        </span><span class="kwd">print</span><span class="pln"> </span><span class="str">'Starting the thread...........'</span><span class="pln"><br>
        run_monitor_object</span><span class="pun">.</span><span class="pln">start</span><span class="pun">()</span><span class="pln"><br>        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">run_timer </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">False</span><span class="pln"><br>
<br>    </span><span class="kwd">if</span><span class="pln"> run_monitor_object</span><span class="pun">.</span><span class="pln">isAlive</span><span class="pun">():</span><span class="pln"><br>        </span><span class="kwd">print</span><span class="pln"> </span><span class="str">'Thread Alive...'</span><span class="pln"><br>
    </span><span class="kwd">else</span><span class="pun">:</span><span class="pln"><br>        </span><span class="kwd">print</span><span class="pln"> </span><span class="str">'Thread is Dead....'</span><span class="pln"><br>
</span></code></pre><br>Any flaw  in the logic? any other better ways of achieving this?<br><br><br><div class="gmail_quote">On Wed, May 11, 2011 at 2:16 PM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">I'm responding to this on-list on the assumption that this wasn't<br>
meant to be private; apologies if you didn't intend for this to be the<br>
case!<br>
<br>
On Wed, May 11, 2011 at 6:38 PM, vijay swaminathan <<a href="mailto:swavijay@gmail.com">swavijay@gmail.com</a>> wrote:<br>
> so If i understand correctly, once the run method of the thread is executed,<br>
> the thread is no more alive.<br>
<br>
Once run() finishes executing, the thread dies.<br>
<br>
> Actually, I'm trying to invoke a command prompt to run some script and as<br>
> long as the script runs on the command prompt, I would like to have the<br>
> thread alive. But according to your statement, the thread would die off<br>
> after invoking the command prompt. is there a way to keep the thread active<br>
> till I manually close the command prompt?<br>
<br>
That depends on how the "invoke command prompt" function works.<br>
<br>
> A snippet of the code written is:<br>
> # Thread definition<br>
> class RunMonitor(QThread):<br>
>     def __init__(self, parent=None):<br>
>         QThread.__init__(self)<br>
>     def run(self):<br>
>         print 'Invoking Command Prompt..........'<br>
>         subprocess.call(["start", "/DC:\\Scripts",<br>
> "scripts_to_execute.bat"], shell=True)<br>
><br>
>  def sendData(self):<br>
><br>
>         if self.run_timer:<br>
>             run_monitor_object = RunMonitor()<br>
>             print 'Starting the thread...........'<br>
>             run_monitor_object.start()<br>
>             self.run_timer = False<br>
><br>
>         if run_monitor_object.isAlive():<br>
>             print 'Thread Alive...'<br>
>         else:<br>
>             print 'Thread is Dead....'<br>
><br>
<br>
subprocess.call() will return immediately, so this won't work. But if<br>
you use os.system() instead, then it should do as you intend.<br>
<br>
> to check the status of the thread repeatedly I have the QTimer which would<br>
> call the self.sendData() for every minute.<br>
><br>
>         self.timer = QTimer()<br>
>         self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData)<br>
>         self.timer.start(1000)<br>
<br>
I'm not really sure what your overall goal is. Can you explain more of<br>
your high-level intentions for this program? There may be a much<br>
easier way to accomplish it.<br>
<br>
Chris Angelico<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Vijay Swaminathan<br>