[BangPypers] Restart when python script hangs
Hello, When I run a python script it hangs at random places. Is there any way to identify when the script hangs and to resume it automatically. When I see that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command to start it again. I want to automate this. Kindly help me how it can done Regards, Rahul G ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Restart when python script hangs
You might need to find first where it hangs. You can do this using test cases. Also if you give some code snippet, we might able to help you. Have a good luck On Mon, Jun 2, 2014 at 3:14 PM, Rahul Gopan rahulpce...@gmail.com wrote: Hello, When I run a python script it hangs at random places. Is there any way to identify when the script hangs and to resume it automatically. When I see that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command to start it again. I want to automate this. Kindly help me how it can done Regards, Rahul G ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Restart when python script hangs
hi rahul, as rohit mentioned, it would be helpful, if you can share the code of the script in question you can share upload the script here https://gist.github.com/ cheers, ashish *The only way to do great work is to love what you do. If you haven’t found it yet, keep looking. Don’t settle. As with all matters of the heart, you’ll know when you find it.” - Steve Jobs (1955 - 2011)* On Mon, Jun 2, 2014 at 4:13 PM, Rohit Chormale rohitchorm...@gmail.com wrote: You might need to find first where it hangs. You can do this using test cases. Also if you give some code snippet, we might able to help you. Have a good luck On Mon, Jun 2, 2014 at 3:14 PM, Rahul Gopan rahulpce...@gmail.com wrote: Hello, When I run a python script it hangs at random places. Is there any way to identify when the script hangs and to resume it automatically. When I see that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command to start it again. I want to automate this. Kindly help me how it can done Regards, Rahul G ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Restart when python script hangs
On Mon, Jun 02 2014, Rahul Gopan wrote: Hello, When I run a python script it hangs at random places. Is there any way to identify when the script hangs and to resume it automatically. When I see that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command to start it again. I want to automate this. From the zen of python Errors should never pass silently. You should find out why it's hanging and fix it. [...] -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Restart when python script hangs
On Mon, Jun 2, 2014 at 3:14 PM, Rahul Gopan rahulpce...@gmail.com wrote: Hello, When I run a python script it hangs at random places. Is there any way to identify when the script hangs and to resume it automatically. When I see that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command to start it again. I want to automate this. As others have already suggested it is not a good idea to silently ignore the errors, so you should debug your script to understand where exactly the script would hang. You can get a trace of the script state when you press 'Ctrl+Z' by handling the signal and making use of the 'inspect' module. Consider the below sample code: % cat signal.py import signal import time import inspect # Register function 'signal_handler' to handle the signal 'Ctrl+Z' signal.signal(signal.SIGTSTP, signal_handler) def signal_handler(signal, frame): print You pressed Ctrl+Z\n; frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1] print(frame,filename,line_number,function_name,lines,index) def signal_check(): while (1): time.sleep(5) if __name__ == '__main__': signal_check() When the above script is running, pressing 'Ctrl+Z' would yield below output and the script would continue its execution. % python signal.py Hello ... You pressed Ctrl+Z (frame object at 0x1a7ae770, 'signal.py', 16, 'signal_check', [' time.sleep(5)\n'], 0) Hello Hello ... The output from inspect: frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1] will give details like name of the function which resulted in call for signal handler, line number etc. Refer to the documentation of 'signal' module for more details on using this module for signal handling. https://docs.python.org/2/library/signal.html#module-signal reg, sateesh ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Restart when python script hangs
If by any chance you are trying to write a deamon, you could monitor it using http://supervisord.org/ On Mon, Jun 2, 2014 at 6:56 PM, Sateesh Kumar sateeshpy...@gmail.com wrote: On Mon, Jun 2, 2014 at 3:14 PM, Rahul Gopan rahulpce...@gmail.com wrote: Hello, When I run a python script it hangs at random places. Is there any way to identify when the script hangs and to resume it automatically. When I see that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command to start it again. I want to automate this. As others have already suggested it is not a good idea to silently ignore the errors, so you should debug your script to understand where exactly the script would hang. You can get a trace of the script state when you press 'Ctrl+Z' by handling the signal and making use of the 'inspect' module. Consider the below sample code: % cat signal.py import signal import time import inspect # Register function 'signal_handler' to handle the signal 'Ctrl+Z' signal.signal(signal.SIGTSTP, signal_handler) def signal_handler(signal, frame): print You pressed Ctrl+Z\n; frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1] print(frame,filename,line_number,function_name,lines,index) def signal_check(): while (1): time.sleep(5) if __name__ == '__main__': signal_check() When the above script is running, pressing 'Ctrl+Z' would yield below output and the script would continue its execution. % python signal.py Hello ... You pressed Ctrl+Z (frame object at 0x1a7ae770, 'signal.py', 16, 'signal_check', [' time.sleep(5)\n'], 0) Hello Hello ... The output from inspect: frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1] will give details like name of the function which resulted in call for signal handler, line number etc. Refer to the documentation of 'signal' module for more details on using this module for signal handling. https://docs.python.org/2/library/signal.html#module-signal reg, sateesh ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers -- A-M-I-T S|S ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Restart when python script hangs
Sateesh, I have a question. In your sample code you are registering the method signal_handler prior to its definition. Does that work? --SSK. On Monday, 2 June 2014 6:57 PM, Sateesh Kumar sateeshpy...@gmail.com wrote: On Mon, Jun 2, 2014 at 3:14 PM, Rahul Gopan rahulpce...@gmail.com wrote: Hello, When I run a python script it hangs at random places. Is there any way to identify when the script hangs and to resume it automatically. When I see that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command to start it again. I want to automate this. As others have already suggested it is not a good idea to silently ignore the errors, so you should debug your script to understand where exactly the script would hang. You can get a trace of the script state when you press 'Ctrl+Z' by handling the signal and making use of the 'inspect' module. Consider the below sample code: % cat signal.py import signal import time import inspect # Register function 'signal_handler' to handle the signal 'Ctrl+Z' signal.signal(signal.SIGTSTP, signal_handler) def signal_handler(signal, frame): print You pressed Ctrl+Z\n; frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1] print(frame,filename,line_number,function_name,lines,index) def signal_check(): while (1): time.sleep(5) if __name__ == '__main__': signal_check() When the above script is running, pressing 'Ctrl+Z' would yield below output and the script would continue its execution. % python signal.py Hello ... You pressed Ctrl+Z (frame object at 0x1a7ae770, 'signal.py', 16, 'signal_check', [' time.sleep(5)\n'], 0) Hello Hello ... The output from inspect: frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1] will give details like name of the function which resulted in call for signal handler, line number etc. Refer to the documentation of 'signal' module for more details on using this module for signal handling. https://docs.python.org/2/library/signal.html#module-signal reg, sateesh ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Restart when python script hangs
On Tue, Jun 3, 2014 at 9:18 AM, Senthil skksun...@yahoo.co.in wrote: Sateesh, I have a question. In your sample code you are registering the method signal_handler prior to its definition. Does that work? --SSK. You are right. The registration of signal handler should be after the function definition. I made some mistake during copy of the sample code. Sorry for the confusion and thanks for the correction. reg, sateesh ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers