Ros node does not shutdown clean
I have the following code running. However when I shutdown the node, the computer always escalates to SIGKILL which takes a long time. This seems to be coming from the loop and reading out the files. If I just print a random message and skip the loop it works. Where is the error and how could I handle it? Here is the code:
#!/usr/bin/env python2
import cv2
import glob
import os
import rospy
from sensor_msgs.msg import Image
if __name__ == "__main__":
rospy.init_node('Publisher', anonymous=True)
hz = rospy.get_param("~rate", 3)
rate = rospy.Rate(hz)
ImgRaw = rospy.Publisher('/camera_topic', Image, queue_size=10)
img_files = "/image_folder/*.jpg"
sort_images = sorted(glob.glob(img_files))
while not rospy.is_shutdown():
for i, filename in enumerate(sort_images, 0):
print("Publish Image with Name: %s" %os.path.basename(filename))
rate.sleep()
Asked by Peter1 on 2019-08-08 03:20:51 UTC
Answers
I think, what coul happen is: Once you terminate, rospy.is_shutdown()
is False. And the currently waiting rate.sleep()
is also terminated. But your for-loop still runs into the next rate.sleep()
.
You could do something like:
while not rospy.is_shutdown():
for i, filename in enumerate(sort_images, 0):
print("Publish Image with Name: %s" %os.path.basename(filename))
rate.sleep()
if rospy.is_shutdown():
break
Let me know if it worked
Asked by ct2034 on 2019-08-08 09:35:28 UTC
Comments
How do you shut down the node? And what is it even doing? Probably the publishing part is missing, right?
Asked by ct2034 on 2019-08-08 05:12:14 UTC
I shut it down with ctrl+c. this is a mwe, where i deleted all the processing and publishing in the pipeline. to show that the loading of the images with glob in the loop somehow leads to this behavior.
Asked by Peter1 on 2019-08-08 06:45:01 UTC