0

I have made a GUI menu with options to start and shut down the python scripts. So far the start button, I am able to run multiple scripts with threading but for the Stop button function, I am still lost.

import time
import threading
from threading import Thread
import rospy
from std_msgs.msg import *
from Tkinter import *
from subprocess import call
import subprocess


data_pub = rospy.Publisher('setup', String, queue_size=10)

def start():
    #global arduino
    #global arm
    #arduino = Popen("/home/ubuntu/catkin_ws/src/tp/scripts/arduino_io_reader1.py")
    #arm = Popen("/home/ubuntu/catkin_ws/src/tp/scripts/move_arm.py")
    global t1 , t2

    t1=threading.Thread(
        target=call,
        args=("/home/ubuntu/catkin_ws/src/tp/scripts/arduino_io_reader1.py",), )
    t1.start()
    t2=threading.Thread(
        target=call,
        args=("/home/ubuntu/catkin_ws/src/tp/scripts/move_arm.py",),)
    t2.start()

def stop():
    global t1 , t2
    #root.destroy()
    #print ("Shuttin down..")
    t1._stop()
    t2._stop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Ashton
  • 1
  • 1
  • Does [this](https://stackoverflow.com/questions/57677228/i-want-to-run-and-kill-a-thread-on-a-button-press/57678641) answer your question? – BeRT2me May 05 '22 at 01:39
  • I would suggest using a `threading.Event` to stop them as illustrated in this [answer](https://stackoverflow.com/a/43686996/355230) to a related question. – martineau May 05 '22 at 01:57
  • @martineau Hi I do not quite understand it. Is there a way to stop or interrupt the code from running and put it into the stop function after I started the start button? The start button is able to run both codes unfortunately not for the stop button. – Ashton May 05 '22 at 03:15
  • How a thread gets started or stopping it is triggered (i.e. by clicking on a GUI button) isn't relevant. There's no official Python API for killing one. Some of the answers to the linked duplicate questions describe ways of achieving that goal by other means. For instance, if you have control over the code running in a thread, there are ways of communicating to it that it should stop — i.e. via a `threading.Event` or `threading.Condition` object. – martineau May 05 '22 at 08:31

0 Answers0