0

with PyQt I've made a GUI (gui.py) with a "start" and a "stop" button. Now I would like to start a another python script (streamData.py), which is in same directory as the GUI script, by pressing the start button. The streamData script is logging some sensor-Data to a file. If I press "stop" button the execution of the streamData script should be terminated.

What is the best way to achieve this? Should I import the streamData.py in my gui script? Or should I use some kind of processing module to start streamData as own process?

The streamData script has following functions inside:

def ctrl_c_handler(signal, frame, node)
    raise Exception("")

node = someNode()

def main():
    signal.signal(signalSIGINT, ctrl_c_handler)
    while 1:
        node.stream()

if __name__ == "__main__":
    main()
    if printInformation: print("disconnected")
    node.disconnect()
    time.sleep(1)
    exit(0)

So is there a way to get the script execution in node.stream let jump to

node.disconnect()
time.sleep(1)
exit(0)

?

Mr Pickel
  • 95
  • 10
  • It all depends on your use case. The easiest would probably be to do it with simple threading. Search SO on how to run concurrent threads in Python, there is more than enough info on that. – zwer May 16 '18 at 10:25
  • @zwer: I've searched here on threading, but as I've figured out there is no way to terminate a thread externally, only to let it terminate itself. as seen: https://stackoverflow.com/questions/6524459/stopping-a-thread-after-a-certain-amount-of-time – Mr Pickel May 16 '18 at 10:39
  • You can add logic to your thread to make it terminate from the outside, and you can run your sensor logging directly in it, or as a subprocess if you really are looking for forcible termination. Either way, you'd want to separate the context from your GUI as you don't want to freeze your GUI while a task is terminated and that's where threads kick in. – zwer May 16 '18 at 11:04

0 Answers0