Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
On Friday, 12 February 2021 16:18:22 GMT Ralph Corderoy wrote: > This is still the counting trace which writes at the end? Yes. The 'live' trace generates around 40 MB of data in around 30 seconds It also slows the program so that the other two programs can't connect > It looks like whatever considers minstermusic.py a ‘job’ is catching all > exceptions, including the SystemExit raised by sys.exit(). So you need > to look at its documentation to Sorry can you define 'its' in this context? This is the code at the end of the program to catch the expected exceptions: #Catch any unexpected errors and log them so we know what happened. except Exception: logger.critical("Unexpected error \n\n"+str(traceback.format_exc()) +"\n\nwhile running. Exiting...") print("Unexpected error \n\n"+str(traceback.format_exc())+"\n\nwhile running. Exiting...") bells_socket.wait_for_handler_to_exit() bells_socket.reset() That was also code written by Hamish for the River System Pis. -- Terry Coles -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
Hi Terry, > The module sys.exit() looks useful, but I keep getting: > > Job "check_webserver_commands (trigger: interval[0:00:01], paused)" > raised an exception > Traceback (most recent call last): > File > "/usr/local/lib/python3.7/dist-packages/apscheduler/executors/base.py", line > 125, in run_job > retval = job.func(*job.args, **job.kwargs) > File "minstermusic.py", line 444, in check_webserver_commands > sys.exit() > SystemExit > > but it doesn't actually exit the program and I have to Ctrl-C to get out > fully, which means it doesn't save the trace. This is still the counting trace which writes at the end? It looks like whatever considers minstermusic.py a ‘job’ is catching all exceptions, including the SystemExit raised by sys.exit(). So you need to look at its documentation to - Confirm it's catching the exception because your contract with it is you won't raise SystemExit. - Find out its defined way of having your job indicate it's finished and shouldn't be called any more. Hopefully, it's the only job and then the job-runner will exit. -- Cheers, Ralph. -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
On Friday, 12 February 2021 14:44:14 GMT Ralph Corderoy wrote: > There's also sys.exit(). See > https://docs.python.org/3/library/exceptions.html#SystemExit Ralph, Thanks for all the foregoing. The module sys.exit() looks useful, but I keep getting: Job "check_webserver_commands (trigger: interval[0:00:01], paused)" raised an exception Traceback (most recent call last): File "/usr/local/lib/python3.7/dist-packages/apscheduler/executors/base.py", line 125, in run_job retval = job.func(*job.args, **job.kwargs) File "minstermusic.py", line 444, in check_webserver_commands sys.exit() SystemExit but it doesn't actually exit the program and I have to Ctrl-C to get out fully, which means it doesn't save the trace. Any ideas? -- Terry Coles -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
Hi Terry, > Immediately after successfully terminating, the MP3 Player restarts > and I can't see why. Whatever starts it the first time is running again. > python3 -m trace --count -C . minstermusic.py > > Unfortunately, I get no output from the trace command because the program is > designed to never finish And because you chose an annotated listing with a count of the number of times each line was run which is only known at the end of the program. It also isn't what you want, as you say below. You want a trace and that shows up as the program is running. $ cat terry.py #! /usr/bin/python def mul(a, b): return a * b for n in range(3): print(n, mul(6, 7)) $ $ ./terry.py 0 42 1 42 2 42 $ $ python -m trace -t terry.py --- modulename: terry, funcname: terry.py(3): def mul(a, b): return a * b terry.py(5): for n in range(3): terry.py(6): print(n, mul(6, 7)) --- modulename: terry, funcname: mul terry.py(3): def mul(a, b): return a * b 0 42 terry.py(5): for n in range(3): terry.py(6): print(n, mul(6, 7)) --- modulename: terry, funcname: mul terry.py(3): def mul(a, b): return a * b 1 42 terry.py(5): for n in range(3): terry.py(6): print(n, mul(6, 7)) --- modulename: terry, funcname: mul terry.py(3): def mul(a, b): return a * b 2 42 terry.py(5): for n in range(3): $ > Is there a way in Python to leave the running software cleanly by > simply executing a Python statement? $ python -c 'raise SystemExit; print(42)' $ echo $? 0 $ python -c 'raise SystemExit(1); print(42)' $ echo $? 1 There's also sys.exit(). See https://docs.python.org/3/library/exceptions.html#SystemExit -- Cheers, Ralph. -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
On Friday, 12 February 2021 10:45:46 GMT Keith Edmunds wrote: > Let us know how you get on. Well pdb was certainly a help, in that I could single step to the point where the MP3 object (mp3_player) was terminated. At that point pdb was abandoned by the software and the Music Player simply started again. This baffled me for an hour or two, but then I realised that my problem was that nearly everything I do in this software is interrupt driven. So when I press the 'Stop Music' Button, this sends a message from the Flask App to the Music Player software. This message is retrieved on a regular basis by an interrupt raised by apscheduler and the MP3 object is terminated in the function mp3_player_stop(). This means of course that once the interrupt is serviced, execution is returned to the mp3_player_start() program which is in a while() loop and testing to see if the player has terminated, using: mp3_wait = mp3_player.wait() if not mp3_wait: print ('MP3 Player: mpg123 failed:%#x\n' % mp3_wait) at the end of the loop. At this point execution goes back to the start of the while(). I put a break after the last line above and the program stopped. :-) However, this only works once at the moment, so I've obviously got a variable somewhere that needs cleaning out, but at least I have some tools to help me do it. Thanks Keith for the suggestion. -- Terry Coles -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
On Friday, 12 February 2021 11:07:05 GMT PeterMerchant wrote: > I have used the Logger function in Python on the Raspberry Pi to see what my > program is doing. Unfortunately I was unable to get logger working with the > timestamp, but it did write to a logfile for analysis. We have custom logging for this program already implemented; courtesy of Hamish, who produced it originally for the River System. Unfortunately, the logger would have to write after every line of code to be useful. I'm getting some progress using pdp. I don't know the answer yet, but I am slowly homing in on the point where the mp3 player gets restarted. -- Terry Coles -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
Alternatively, is there a way to get trace to do it's stuff while the program is executing? I have used the Logger function in Python on the Raspberry Pi to see what my program is doing. Unfortunately I was unable to get logger working with the timestamp, but it did write to a logfile for analysis. Peter -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
On Friday, 12 February 2021 10:45:46 GMT Keith Edmunds wrote: > Have you tried debugging with pdb? I'd put a breakpoint where the code > stops the player, then step through it from there. If you're not familiar > with that technique it would be helpful for you to understand the > difference between the debugger commands 'next' and 'step'. Thanks Keith that looks as if it might do the job. -- Terry Coles -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
Have you tried debugging with pdb? I'd put a breakpoint where the code stops the player, then step through it from there. If you're not familiar with that technique it would be helpful for you to understand the difference between the debugger commands 'next' and 'step'. Let us know how you get on. -- Linux Tips: https://www.tiger-computing.co.uk/category/techtips/ -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
[Dorset] Terminate a Python Program Conditionally (or Alternatively Trace a Running Program)
Hi, My Music Player software works fine and I'm in the process of debugging the Web App. One of the buttons in the Control Page is to stop the music and when pressed, the mp3 object is terminated successfully, This has worked now for four years in the original program (using a physical switch connected to a GPIO pin rather than a Web App). Unfortunately, now I've added the Web code, I've hit a snag. Immediately after successfully terminating, the MP3 Player restarts and I can't see why. I've tried adding print statements to the code and this has confirmed all of the above, but that's not enough. The obvious answer is to trace the program, so I had a look at: http://www.chiark.greenend.org.uk/doc/python3.2/html/library/trace.html and tried to use the simple example given, eg: python3 -m trace --count -C . minstermusic.py Unfortunately, I get no output from the trace command because the program is designed to never finish so I have to Ctrl-C to get out of it. Is there a way in Python to leave the running software cleanly by simply executing a Python statement? If so I could put that into my program to see where the execution goes after the mp3_player_stop() function has completed. I've been unable to find such a statement. Alternatively, is there a way to get trace to do it's stuff while the program is executing? -- Terry Coles -- Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk