Ask Your Question
0

Ros node does not shutdown clean

asked 2019-08-08 03:20:51 -0500

Peter1 gravatar image

updated 2019-08-08 03:51:57 -0500

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()
edit retag flag offensive close merge delete

Comments

How do you shut down the node? And what is it even doing? Probably the publishing part is missing, right?

ct2034 gravatar imagect2034 ( 2019-08-08 05:12:14 -0500 )edit

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.

Peter1 gravatar imagePeter1 ( 2019-08-08 06:45:01 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-08-08 09:35:28 -0500

ct2034 gravatar image

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

edit flag offensive delete link more

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

Stats

Asked: 2019-08-08 03:20:51 -0500

Seen: 25 times

Last updated: Aug 08