简体   繁体   中英

Ctrl-C does not kill my python code due to threading problems

I am using Flask as a local server and initiating a new thread:

rospy.init_node('path_planner')

As I am initiating this thread on a main thread, when I press Ctrl-C, nothing happens and I have to manually kill the process using kill -9

I have tried to signal_handler but still I was not able to kill my program.

Here is my code for POST method, which is used often:

app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://ed:123@ds029227.mlab.com:2325/test"

mongo = PyMongo(app)

@app.route('/goal', methods=['POST'])
def add_goal():
    goal = mongo.db.goal
    position = request.json['position']
    orientation = request.json['orientation']
    goal_id = goal.insert({'position' : position, 'orientation' : 
orientation})
    new_goal = goal.find_one({'_id' : goal_id})
    output = {'position' : new_goal['position'], 'orientation' : 
new_goal['orientation']}
    position_x = json.loads(position['x'])
    position_y = json.loads(position['y'])

    return jsonify(output)

And here is my main:

if __name__ == '__main__':
    rospy.init_node("path_planner")
    app.run(debug=True)

When I run my code, flask server fires up and everything works as expected, POST method does its job. However, when I am done and I need to exit the program, I press Ctrl-C but nothing appears to happen.

try to press the break button. It will break the process I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM