14 Replies Latest reply on May 25, 2015 2:26 PM by Intel_Peter

    python script hangs when using mraa

    mpenna

      This is kind of strange and I haven't found any information or other reports on this so far. I hope it's some screw up on my side, though, otherwise it's some odd bug on mraa's side. I'm working on a script in Python for a background service that will rely on the mraa library for handling GPIO -- nothing fancy, just two buttons for now. The strange thing I noticed is that upon exiting (e.g. Ctrl-C) my service logic is being tore down properly but the script itself however never exits. Only after issuing a Ctrl-Z that the script does indeed exit. If anyone is interested in trying this out, I have attached two almost identical test scripts, except that one uses mraa and the other one does not. The expected behavior running the scripts is that both will print "running" every 5s to the console and the mraa one will print a corresponding message whenever either one of the two buttons is pressed. Upon issuing Ctrl-C to end the scripts, one will exit properly to the console while the other will just hang. I'm using the Edison mini-breakout board.

       

      I hope someone can make something out of this.

       

      regards

        • 1. Re: python script hangs when using mraa
          Intel_Peter

          Hello mpenna,

           

          Which version of MRAA do you have installed on your board? Do you have the latest image on your Edison? I'd like to test this on your same conditions.

           

          Peter.

          • 2. Re: python script hangs when using mraa
            mpenna

            Hi Peter,

             

            Thanks for jumping in...  I'm using, I believe, Edison's latest image, the one I got at the following link: http://downloadmirror.intel.com/24698/eng/edison-image-ww05-15.zip

             

            Issuing a configure_edison --version command returns 120.

             

            mraa's version is 0.6.1

             

            regards

            • 3. Re: python script hangs when using mraa
              mpenna

              Hey Peter... were you able to reproduce the issue? Any ideas why this could be happening?

              • 4. Re: python script hangs when using mraa
                Intel_Peter

                Yes, I was able to replicate your issue on my Edison, I checked your code, and I found that since you have programmed the keyboard interrupt, it doesn't make the Edison close the Python enviroment, so I added a line to your code, it worked for me, why don't you give it a shot.

                 

                #!/usr/bin/python
                import threading
                import sys
                import time
                import signal
                import mraa
                # mraa: 46 / edison: GP47 / mini-breakout board: J20-5
                # mraa: 47 / edison: GP49 / mini-breakout board: J20-6
                lock = threading.Lock()
                running = True
                button_prev = mraa.Gpio(mraa.INTEL_EDISON_MINIBOARD_J20_5)
                button_next = mraa.Gpio(mraa.INTEL_EDISON_MINIBOARD_J20_6)
                def sigterm_handler(signo, stack_frame):
                    # raises SystemExit(0)
                    print "termination signal received"
                    sys.exit(0)
                def button_prev_press(args):
                    # filter out the false positives (emi interference, power fluctuations, etc)
                    time.sleep(0.2)
                    if not button_prev.read():
                        return
                    # if we get here it's probably a true button press
                    print "prev button pressed"
                def button_next_press(args):
                    # filter out the false positives (emi interference, power fluctuations, etc)
                    time.sleep(0.2)
                    if not button_next.read():
                        return
                    # if we get here it's probably a true button press
                    print "next button pressed"
                def run():
                    while running:
                        print "running"
                        time.sleep(5)
                def stop():
                    global running
                    with lock:
                        running = False
                def main():
                    at = threading.Thread(target=run)
                    button_prev.dir(mraa.DIR_IN)
                    button_prev.isr(mraa.EDGE_RISING, button_prev_press, None)
                    button_next.dir(mraa.DIR_IN)
                    button_next.isr(mraa.EDGE_RISING, button_next_press, None)
                    try:
                        at.start()
                        while True:
                            time.sleep(0.2)
                    except KeyboardInterrupt as e:
                        print "Keyboard interrupt: %s" % str(e)
                    except OSError as e:
                        print "OS error: %s" % str(e)
                    except Exception as e:
                        print "Other error: %s" % str(e)
                    finally:
                        stop()
                        at.join()
                        print "Bye"
                        exit()          #<----------------------I added this line
                if __name__ == '__main__':
                    signal.signal(signal.SIGTERM, sigterm_handler)
                    main()
                
                
                

                 

                Peter.

                • 5. Re: python script hangs when using mraa
                  mpenna

                  I must be experiencing some other problem then, since that didn't do the trick for me. I've attached an image showing a print screen of my console window with the output I get whenever I run the the 'mraa' test script. This is the same logic flow that I use on a couple of other services I've built that run OK on the Raspberry Pi platform (Raspbian OS), adapted however for the Edison's own environment and libraries, of course.

                  4-9-2015 6-02-35 PM.png

                  • 6. Re: python script hangs when using mraa
                    Intel_Peter

                    That's weird,I just tried it with another Edison and it worked again with no problems, have updated your python version? Which image are you on?

                     

                    Peter.

                    • 7. Re: python script hangs when using mraa
                      mpenna

                      When I issue the "configure_edison --version" command, I get just "120" back.

                       

                      Regarding Python... I'm running version 2.7.3. This is what shows up when I start the REPL:

                       

                      root@edison1:~# /usr/bin/python

                      Python 2.7.3 (default, Dec  9 2014, 18:06:54)

                      [GCC 4.8.2] on linux2

                      Type "help", "copyright", "credits" or "license" for more information.

                      >>>

                       

                      One really strange thing, though, and perhaps it's somehow related to the problem at hand, is that whenever I run the REPL, be it either via serial or via SSH, whenever I exit the REPL (Ctrl-z), the terminal apparently gets stuck -- no longer it responds to keystrokes. I usually have to restart/reconnect the terminal or sometimes I even have to go as far as restarting the Edison. Certainly there's something really weird going on. Hope this extra info helps.

                      • 8. Re: python script hangs when using mraa
                        mhahn

                        which shell do you use? Can you try with bash? (you probably have to install)

                        • 9. Re: python script hangs when using mraa
                          mpenna

                          Hi mhahn,

                           

                          I'm not sure which shell I was using before... the default one for the current Edison image, I guess...  but I went ahead and installed bash following your suggestion:

                           

                          01 - bash installation.png

                           

                          Not sure If I have to do anything else to make bash "stick", so to speak, but anyway, gave a shot again at our test script:


                          02 - python script hangs.png

                           

                          As you can see, the same problem still presents itself... but again, not sure if bash is the shell being used here... maybe you can shed some light on how to make sure bash is the one to be used. 

                          • 10. Re: python script hangs when using mraa
                            mhahn

                            not sure whether it is related to the shell - I just remember having seen a maybe slightly similar issue related to a shell.

                            To figure out the shell used just type "echo $SHELL".

                            To use bash (if not load automatically - which you could e.g. set in /etc/passwd): just type "bash"

                            • 11. Re: python script hangs when using mraa
                              mpenna

                              So apparently I wasn't using bash on the previous attempt:

                               

                              root@edison1:~# echo $SHELL

                              /bin/sh

                               

                              I went ahead and issued the following command, which, I believe, has configured bash as the default shell:

                               

                              chsh -s /bin/bash root

                               

                              Rebooted and now this is what I get:

                               

                              root@edison1:~# echo $SHELL

                              /bin/bash

                               

                              But... much to my regret, the problem still persists. The script still hangs on exiting.

                              • 12. Re: python script hangs when using mraa
                                mpenna

                                Not exactly the most elegant of solutions, and I'm still not completely sure that its because of this that the script is now exiting/ending, but apparently I've managed to overcome this hanging problem by issuing a kill command before exiting the script, passing the own process ID of the currently running script.

                                 

                                So now, my test test-11-mraa.py looks like this:

                                 

                                #!/usr/bin/python
                                import os
                                import threading
                                import sys
                                import time
                                import signal
                                import mraa
                                
                                # mraa: 46 / edison: GP47 / mini-breakout board: J20-5
                                # mraa: 47 / edison: GP49 / mini-breakout board: J20-6
                                
                                lock = threading.Lock()
                                pid = os.getpid()     # <-----  get process id of script
                                running = True
                                button_prev = mraa.Gpio(mraa.INTEL_EDISON_MINIBOARD_J20_5)
                                button_next = mraa.Gpio(mraa.INTEL_EDISON_MINIBOARD_J20_6)
                                
                                
                                def sigterm_handler(signo, stack_frame):
                                    # raises SystemExit(0)
                                    print "termination signal received"
                                    sys.exit(0)
                                
                                
                                def button_prev_press(args):
                                    # filter out the false positives (emi interference, power fluctuations, etc)
                                    time.sleep(0.2)
                                    if not button_prev.read():
                                        return
                                    # if we get here it's probably a true button press
                                    print "prev button pressed"
                                
                                
                                def button_next_press(args):
                                    # filter out the false positives (emi interference, power fluctuations, etc)
                                    time.sleep(0.2)
                                    if not button_next.read():
                                        return
                                    # if we get here it's probably a true button press
                                    print "next button pressed"
                                
                                
                                def run():
                                    while running:
                                        print "running"
                                        time.sleep(5)
                                
                                
                                def stop():
                                    global running
                                    with lock:
                                        running = False
                                
                                
                                def main():
                                    at = threading.Thread(target=run)
                                
                                    button_prev.dir(mraa.DIR_IN)
                                    button_prev.isr(mraa.EDGE_RISING, button_prev_press, None)
                                    button_next.dir(mraa.DIR_IN)
                                    button_next.isr(mraa.EDGE_RISING, button_next_press, None)
                                
                                    try:
                                        at.start()
                                
                                        while True:
                                            time.sleep(0.2)
                                
                                    except KeyboardInterrupt as e:
                                        print "Keyboard interrupt: %s" % str(e)
                                
                                    except OSError as e:
                                        print "OS error: %s" % str(e)
                                
                                    except Exception as e:
                                        print "Other error: %s" % str(e)
                                
                                    finally:
                                        stop()
                                
                                        at.join()
                                
                                        print "Bye"
                                
                                        # exit()
                                        # sys.exit(0)
                                
                                        os.kill(pid, 1)     # <----- kill own process
                                
                                
                                if __name__ == '__main__':
                                    signal.signal(signal.SIGTERM, sigterm_handler)
                                
                                    main()
                                
                                
                                
                                
                                • 13. Re: python script hangs when using mraa
                                  mhahn

                                  arfoll - any thoughts?

                                  • 14. Re: python script hangs when using mraa
                                    Intel_Peter

                                    Hello mpenna,

                                     

                                    Why don't you try with release 2.1 and see if there's any difference?

                                     

                                    Peter.