Re: NewBie Doubt in Python Thread Programming
On Thu, May 12, 2011 at 3:35 PM, vijay swaminathan swavi...@gmail.com wrote: I tried using QThread as well.. But the problem is, on the run method when i invoke the command prompt, it sends out the finished signal... I want it to send out the finished signal only on closing the command prompt that is invoked earlier in my process. subprocess.call([start, /DC:\\PerfLocal_PAL, scripts_to_execute.bat], shell=True) This is your problem, still. You need to change to a call that waits. In my testing on Windows (Python 2.6.5), this can be done with os.system() quite happily. Change that, and it should all work. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
Hi Chris, I tried using os.system as well but it did not even open up the command prompt. Can you please share the code that worked for you.. just wondering if I'm missing something very basic. Regards, -Vijay Swaminathan., On Thu, May 12, 2011 at 1:38 PM, Chris Angelico ros...@gmail.com wrote: On Thu, May 12, 2011 at 3:35 PM, vijay swaminathan swavi...@gmail.com wrote: I tried using QThread as well.. But the problem is, on the run method when i invoke the command prompt, it sends out the finished signal... I want it to send out the finished signal only on closing the command prompt that is invoked earlier in my process. subprocess.call([start, /DC:\\PerfLocal_PAL, scripts_to_execute.bat], shell=True) This is your problem, still. You need to change to a call that waits. In my testing on Windows (Python 2.6.5), this can be done with os.system() quite happily. Change that, and it should all work. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
On Thu, May 12, 2011 at 7:42 PM, vijay swaminathan swavi...@gmail.com wrote: Hi Chris, I tried using os.system as well but it did not even open up the command prompt. Can you please share the code that worked for you.. just wondering if I'm missing something very basic. Caveat: I'm not using Qt, I just tried this in IDLE (which is graphical). C:\copy con test.bat echo Hello, world! pause ^Z 1 file(s) copied. IDLE 2.6.5 import os os.system(c:\\test.bat) 0 The 0 and subsequent prompt don't appear until the batch file finishes. Meanwhile, the batch file is executing in a separate window. When you try it inside Qt, do you get a background window that needs to be brought to the front? Does execution pause while the batch file runs? Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
On Wed, May 11, 2011 at 4:57 PM, vijay swaminathan swavi...@gmail.com wrote: for i in range(10): MyThread_Object.start() count = threading.activeCount() When I run this, I could see 10 thread being called. But when I print the active thread count it is only 2. Need some understanding on the following. 1. How the total active thread is 2? My guess is that all (or all but one) of the threads have finished already by the time you check activeCount. If you add a call to time.sleep(1) in the run() method, you'll see 11 or 12 threads (your main threads and 10 others). If you print out threading.activeCount() at the top of the program, you'll see that it starts at 2 in IDLE, or 1 in stand-alone Python. 2. how do I stop a thread? does it get automatically stopped after execution Once the run() method returns, the thread is terminated. You shouldn't normally need to stop a thread from another thread. 3. Am I totally wrong in understanding the concepts. 4. what is the difference between active_count() and activeCount() since both seem to give the same result. The camelCase function names were inspired by Java's API, the ones with underscores are more Python's style. They are absolutely the same though. See the notes at the top of http://docs.python.org/library/threading.html for that and other information. 5. is there a way to find out if the thread is still active or dead? Yep! Call is_alive() on your thread object. It'll return True if it's still going. Again, the docs for the threading module (http://docs.python.org/library/threading.html) have all that sort of thing. Threading is a bit of a tricky concept, and takes some getting used to. There are many places where threads are awesome, and many where they're pretty useless. The place I most often use threads is in socket programming; when I run a server, I usually spin off a thread to handle each incoming socket, as it's the easiest way to handle sequential actions (especially if the socket protocol is command-response, like a MUD or a mail server). Once you get your head around the threading module, you'll find the multiprocessing module very similar. For Python, the difference is sometimes quite important, so it's as well to understand both. Hope that helps! Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
En Wed, 11 May 2011 03:57:13 -0300, vijay swaminathan swavi...@gmail.com escribió: Hi All, I'm new bie to thread programming and I need some assistance in understanding few concepts ... I have a very simple program which runs a thread and prints a string. import threading class MyThread(threading.Thread): def __init__(self, parent = None): threading.Thread.__init__(self) def run(self): print 'Hello World' def main(): for i in range(10): MyThread_Object = MyThread() print 'Object id is : ' , id(MyThread_Object) print 'Staring thread -- ' , MyThread_Object.getName() MyThread_Object.start() count = threading.activeCount() print 'The active thread count is: ' , count if __name__ == '__main__': main() When I run this, I could see 10 thread being called. But when I print the active thread count it is only 2. Need some understanding on the following. 1. How the total active thread is 2? Because most of them have already finished by then. Your run() method executes quite fast. Make it take more time (maybe by adding time.sleep(1)) and you'll see 10 active threads. 2. how do I stop a thread? does it get automatically stopped after execution ? You don't; a trhread runs until the run() method exits. After that, the OS thread finishes. The Python object (a threading.Thread instance) is still alive (until the last reference to it disappears, as any other object). 3. Am I totally wrong in understanding the concepts. I don't know... 4. what is the difference between active_count() and activeCount() since both seem to give the same result. Nothing. active_count is the preferred Python spelling per PEP8; activeCount is the original Java spelling. 5. is there a way to find out if the thread is still active or dead? Yes, use is_alive() -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
On Wed, May 11, 2011 at 4:57 PM, vijay swaminathan swavi...@gmail.com wrote: [...] 1. How the total active thread is 2? Your threads are terminating as normal. Without some kind of loop in your run() method they will execute the instructions and terminate. 2. how do I stop a thread? does it get automatically stopped after execution Usually by a flag or condition that terminates your run() function/method. 3. Am I totally wrong in understanding the concepts. 4. what is the difference between active_count() and activeCount() since both seem to give the same result. They are synonyms. 5. is there a way to find out if the thread is still active or dead? See: pydoc threading.Thread or help(threading.Thread) cheers James -- -- James Mills -- -- Problems are solved by method -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
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 swavi...@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
Re: NewBie Doubt in Python Thread Programming
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 ros...@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 swavi...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
On Wed, May 11, 2011 at 7:08 PM, vijay swaminathan swavi...@gmail.com wrote: 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.. No probs. If you just send your response to the list python-list@python.org. it'll get to everyone. 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). Yes, but only if you use os.system(). Any flaw in the logic? any other better ways of achieving this? You'll find it easier to get an event at the end of it; simply have another line of code after the os.system() which will reenable the button. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
Is there any special reason you don't want to use QThread? http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details regards 2011/5/11 Chris Angelico ros...@gmail.com: On Wed, May 11, 2011 at 7:08 PM, vijay swaminathan swavi...@gmail.com wrote: 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.. No probs. If you just send your response to the list python-list@python.org. it'll get to everyone. 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). Yes, but only if you use os.system(). Any flaw in the logic? any other better ways of achieving this? You'll find it easier to get an event at the end of it; simply have another line of code after the os.system() which will reenable the button. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
On Thu, May 12, 2011 at 1:16 AM, Wojtek Mamrak tacyt1...@gmail.com wrote: Is there any special reason you don't want to use QThread? http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details Other than that QThread is part of QT and threading isn't, what are the advantages of QThread? Is it possible (safe) to manipulate QT objects - in this case, the button - from a thread other than the one that created them? (If not, that would be a good reason for using QThread, which will fire an event upon termination.) Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
2011/5/11 Chris Angelico ros...@gmail.com: On Thu, May 12, 2011 at 1:16 AM, Wojtek Mamrak tacyt1...@gmail.com wrote: Is there any special reason you don't want to use QThread? http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details Other than that QThread is part of QT and threading isn't, what are the advantages of QThread? Is it possible (safe) to manipulate QT objects - in this case, the button - from a thread other than the one that created them? (If not, that would be a good reason for using QThread, which will fire an event upon termination.) QThread provides mechanism of signals and slots (from and to the thread), which are used across all pyQt. Unfortunately it is not possible to use any widget classes in the thread (direct quote from the docs). On the other hand signals can fire methods from the main thread (running the app'a main loop), so this is not a big deal. The signals are: - finished - started - terminated It is possible to block the thread, make it sleep, check whether the thread is running, and few others. -- http://mail.python.org/mailman/listinfo/python-list
Re: NewBie Doubt in Python Thread Programming
I tried using QThread as well.. But the problem is, on the run method when i invoke the command prompt, it sends out the finished signal... I want it to send out the finished signal only on closing the command prompt that is invoked earlier in my process. guess some logic to be implement inside run() which monitors the command prompt / reports the status once the command prompt is closed. I tried running loop inside the run() to keep the thread active but no help.. class RunMonitor(QThread): def __init__(self, parent = None): QThread.__init__(self) def run(self): print 'Invoking command prompt...' subprocess.call([start, /DC:\\PerfLocal_PAL, scripts_to_execute.bat], shell=True) while True: pass def runscript(self): print 'Complete_file_Path inside Run script is : ' , self.complete_file_path file_operation.Generate_Bat_File(self.complete_file_path) self.run_monitor_object = RunMonitor() self.run_monitor_object.start() self.connect(self.run_monitor_object, SIGNAL(finished()),self.threadstatus) On Wed, May 11, 2011 at 9:25 PM, Wojtek Mamrak tacyt1...@gmail.com wrote: 2011/5/11 Chris Angelico ros...@gmail.com: On Thu, May 12, 2011 at 1:16 AM, Wojtek Mamrak tacyt1...@gmail.com wrote: Is there any special reason you don't want to use QThread? http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details Other than that QThread is part of QT and threading isn't, what are the advantages of QThread? Is it possible (safe) to manipulate QT objects - in this case, the button - from a thread other than the one that created them? (If not, that would be a good reason for using QThread, which will fire an event upon termination.) QThread provides mechanism of signals and slots (from and to the thread), which are used across all pyQt. Unfortunately it is not possible to use any widget classes in the thread (direct quote from the docs). On the other hand signals can fire methods from the main thread (running the app'a main loop), so this is not a big deal. The signals are: - finished - started - terminated It is possible to block the thread, make it sleep, check whether the thread is running, and few others. -- http://mail.python.org/mailman/listinfo/python-list -- Vijay Swaminathan -- http://mail.python.org/mailman/listinfo/python-list