Menu

#62 wait_for_edge with external thread causes Error #5 waiting for edge

Done
None
High
Patch
2016-02-09
2014-03-21
Kyle
No

I'm using the raspberry pi in conjunction with the camera board to create a DIY photobooth to save some money for my wedding in July. I originally was hammering the gpio pin in a while loop waiting for a button to be pressed, but found a much more elegant solution via the wait_for_edge() function, allowing me to not have to max out my CPU usage.

I ran into a bit of a conundrum though. When my python script starts, it creates a background thread that monitors ink levels every 30 seconds or so. If the ink levels are low, it will email/text/notify specific users so they can attend to the photobooth.

I have found that after executing wait_for_edge(), and the program is waiting to return until that edge is detected, when the ink_level_thread gets out of it's time.sleep() and executes, I get "RuntimeError: Error #5 waiting for edge"

If I disable the ink_level_thread completely, I never experience this RuntimeError #5. When the runtime error does occur, the main program freezes up and doesn't run anymore, and the ink_level_thread just keeps running. The only way I can get the program back to running normally is either reboot the Pi, or 'sudo kill -9 python_pid' which causes the GPIO's to be in a funky state because they weren't cleaned up.

Any idea why this is happening? Thank you for any help.

I'm running RPi.GPIO-0.5.4
Hopefully I provided adequate information. If not, please let me know what else I can provide.

Discussion

  • Kyle

    Kyle - 2014-03-21

    Looking into it, it looks like it occurs when py_wait_for_edge() in py_gpio.c executes blocking_wait_for_edge() in event_gpio.c

    Specifically in event_gpio.c, the epoll_wait(epfd, &events, 1, -1) is where the issue occurs. Looking into it, (I'm new to Python), python is not completely thread safe, which is why before you call blocking_wait_for_edge, you set Py_BEGIN_ALLOW_THREADS to verify you have acquired the lock.

    This verified that my background thread is definitely causing the issue, probably bad programming by me.

    How can I change the structure of my background thread to not conflict with wait_for_edge()? Should I set a mutex that is used by my thread and the wait_for_edge() python call? Or is there a better way?

     
  • Kyle

    Kyle - 2014-03-23

    I implemented a thread lock around wait_for_edge and work done in my thread and have not had an issue with Error #5 yet. Looks like should have hopefully solved my issue.

     
  • Ben Croston

    Ben Croston - 2016-02-09
    • status: New --> Done
    • assigned_to: Ben Croston
     
  • Ben Croston

    Ben Croston - 2016-02-09

    OK - closing the issue. Other approaches which might have worked for you:

    • polling event_detected()
    • the recently added 'timeout' parameter to wait_fior_edge
     

Log in to post a comment.

MongoDB Logo MongoDB