-1

I am making a AI assistant and I ran into a problem.

I have written code for the actual AI in a python file named JARVIS.py. But the GUI for the AI is written in another python file.To compile them, i have made a new .py file named JARVIS_Final.py. Now I have made a button on the gui for closing the entire program. The code for closing is -

self.ui.pushButton.clicked.connect(self.close)

But this doesn't stop everything, it just closes the gui, not the actual AI. What should i do?

The code - https://drive.google.com/file/d/1Tj-133LDw9q-oNQp7vpqivwjR4MkCdmJ/view?usp=sharing

I use Python 3.9, windows machine and i used QTdesiner for the GUI. Sorry for my bad english.

CoderMan
  • 43
  • 6

2 Answers2

2

Without any other details about your situation or samples of your code, the best that can be done is to give you the layout of an approach.

In your code for your AI (JARVIS.py if I follow), acquire the PID of that process.

import os
ai_pid = os.getpid()

Pass that pid into your JARVIS_Final.py or GUI file and in your self.close function (which you may need to override), kill the pid of the other process:

import psutil
p = psutil.Process(pid=ai_pid)
p.terminate()

See also this post about killing processes in Python (which can include other Python processes): How to terminate process from Python using pid?

William
  • 381
  • 1
  • 8
  • Sorry, i forgot to add the code. The link is shared in the edited question now – CoderMan Jun 27 '21 at 09:18
  • Instead of that what I used is - import psutil, os def terminate(): for process in psutil.process_iter(): if process.name() == 'python.exe': os.system("taskkill /f /im " + str(process.pid)) – CoderMan Jun 28 '21 at 05:18
0

you can use subprocess library in python to run and manage other scripts . see this link .

import os
import signal
import subprocess

# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before  exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                   shell=True, preexec_fn=os.setsid) 

os.killpg(os.getpgid(pro.pid), signal.SIGTERM)  # Send the signal to all the process groups
  • You've got a good point, it's not clear how the different python processes are starting the others. Your approach would be good for that. Your approach is definitely going to be needed if the AI process is actual going to be running separately (so as to not stall the Qt event loop), or it could be threaded I guess. – William Jun 27 '21 at 08:33
  • I have put the code (for the ai - JARVIS.py) in a class and have imported the class in Jarvis_Final.py. From the class, a function - run() is used to start the entire AI. I run the run() function in a thread, and the gui lives in the main thread. – CoderMan Jun 27 '21 at 09:08