ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Python rospy Script doesn't end running

asked 2015-02-26 07:28:42 -0600

Holzroller gravatar image

Hey there,

i already talked to some guys in #python irc channel. My aim was to close a while loop with ctrl-c so i used a try - exception statement. The problem is that the while loop doesnt really close. If i put a print "whatever" in the exception statement it doesnt get printed, though the code in the while loop doesnt get executed anymore. People said that my code itself seems correct. So i used the faulthandler to get deeper into that problem and it seems that it has smth to do with ROS. But im not into programming that deeply to understand what is going wrong here or maybe its just a simple mistake i make...

Anyways, here is my code:

#!/usr/bin/env python

import time
import sys

#Ueye Imports
import ids

#ROS Imports
import rospy
from sensor_msgs.msg import Image

#OpenCV Imports
import cv2
from cv_bridge import CvBridge, CvBridgeError

import faulthandler
import signal


# +++++++++++++++ ROS Node Einstellugen +++++++++++++++
# Diese Node agiert unter dem Namen "NeedleDetector"
rospy.init_node('VideoPublisher', anonymous=True)

# und published folgende Topics:
VideoRaw = rospy.Publisher('Kamera_Rohbild', Image, queue_size=10)
EdgesRaw = rospy.Publisher('Kamera_Kantenbild', Image, queue_size=10)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++

cam = cv2.VideoCapture('output.avi')

# ++++++++++++++++ Einstellungen Bildverarbeitung +++++++++++++++
#Einstellungen fuer den Gaussschen Weichzeichner
gaussian_blur_ksize = (5, 5)
gaussian_blur_sigmaX = 4

#Die Threshold Werte fuer den Canny Operator
threshold1 = 55
threshold2 = 75
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
br = CvBridge()

    while cam.isOpened():

        # Naechstes Bild abfragen
        meta, frame =

        # Anwendung des Gausschen Weichzeichners um das Rauschen etwas zu unterdruecken
        frame_gaus = cv2.GaussianBlur(frame, gaussian_blur_ksize, gaussian_blur_sigmaX)

        # ueberfuehren in ein Graustufen Bild fuer den Canny Algorithmus
        frame_gray = cv2.cvtColor(frame_gaus, cv2.COLOR_BGR2GRAY)

        # ueberfuehren in das Kantenbild
        frame_edges = cv2.Canny(frame_gray, 55, 75)

        # umwandeln der Numpy Arrays in die ROS Nachricht
        frame_msg = br.cv2_to_imgmsg(frame, "rgb8")
        frame_msg_edges = br.cv2_to_imgmsg(frame_edges, "mono8")

        # Nachrichten veroeffentlichen


except KeyboardInterrupt:
    print "1"
    print "2"

And here is what the faulthandler said:

Thread 0x00007f8f02b25700:
  File "/usr/lib/python2.7/", line 358 in wait
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/", line 418 in _run
  File "/usr/lib/python2.7/", line 763 in run
  File "/usr/lib/python2.7/", line 810 in __bootstrap_inner
  File "/usr/lib/python2.7/", line 783 in __bootstrap

Thread 0x00007f8eea43b700:
  File "/usr/lib/python2.7/", line 358 in wait
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/", line 418 in _run
  File "/usr/lib/python2.7/", line 763 in run
  File "/usr/lib/python2.7/", line 810 in __bootstrap_inner
  File "/usr/lib/python2.7/", line 783 in __bootstrap

Thread 0x00007f8ee9c3a700:
  File "/usr/lib/python2.7/", line 358 in wait
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/", line 418 in _run
  File "/usr/lib/python2.7/", line 763 in run
  File "/usr/lib/python2.7/", line 810 in __bootstrap_inner
  File ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-02-27 22:04:56 -0600

You are not using rospy.is_shutdown() correctly. Try moving it into the condition of the while-loop. Compare with the example in the tutorial at

edit flag offensive delete link more


oh i see. so i implemented it this way : while cam.isOpened() and not rospy.is_shutdown(): ... and it is working! so if i press ctrl-c now, rospy gets shutdown? Or what does trigger the script to close the ros node?

Holzroller gravatar image Holzroller  ( 2015-02-28 07:13:27 -0600 )edit

Yes, by default Ctrl-C causes an interruption signal (SIGINT ) that is caught by rospy, which then starts a shutdown procedure. You can use different signal handlers or add hooks if you want. Consult

slivingston gravatar image slivingston  ( 2015-02-28 15:34:23 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-02-26 07:28:42 -0600

Seen: 5,095 times

Last updated: Feb 27 '15