NewBie Doubt in Python Thread Programming

Chris Angelico rosuav at gmail.com
Wed May 11 04:46:36 EDT 2011


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



More information about the Python-list mailing list