Python Forum

Full Version: Close script from another script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
hi everyone I'm new to the forum, I wanted to ask if there was a way to close a script from another script
for example:
suppose two scripts in while loop, I would like the first one to reach a certain condition to close the other script.
it's possible?
Well, when you're running a program, your OS creates a process and there are a bunch of inter-process communication (IPC) techniques that can be used (for example, signals, at least on UNIX). It would help to know more about what these programs are and how they interact, what OS you're on, etc.
Yes! This is entirely possible. However, like ndc85430 says, it is dependent on your platform. I can show you how to kill them on both Linux, and Windows. This is achieved through using shell commands, which you can access from the os.system function from the os library.

On Linux, there are two main ways I know of to kill a process, or processes. This is by using the shell commands kill and killall. They both send a signal SIGTERM by default, which essentially asks the process to stop. This also means the process doesn't have to listen to it, so if you need to forcefully kill a process, you can prefix each command with -9 to send a SIGKILL, which will force it to end, but I suggest you don't do this unless it's ABSOLUTELY NECESSARY, as it could cause problems with I/O operations like writing to files.

import os

os.system("kill process_id") # This will send a SIGTERM signal by default.
os.system("kill -9 process_id") # This will send a SIGKILL signal to a process with the id provided.

os.system("killall process_name") # This will send a SIGTERM signal to all processes named process_name. This can be dangerous, so be careful!
os.system("killall -9 process_name") # This will send a SIGKILL signal to all processes named process_name. This is even more dangerous, use this with care!
On Windows, you can use the TASKKILL command to end a process, or forcefully end a process. However, I'm not sure if you can pull off killing all processes of a specific name with the command alone.

import os

os.system("taskkill /pid process_id") # This will kill the process that has this process id.
os.system("taskkill /f /pid process_id") # This will forcefully kill the process that his this process id.
Hope this helps!
Thank you so much for your help it was all very clear
I have another question to ask you even if it deviates from the question.
I've been stuck on this for months, namely:
I create a module.py with an array inside, for example: X = ['1000']
then I create another file_main.py
now in the file_main.py I put the function y = str (input "value:")
now I would like the X in module.py to change to
X = ['1000' + y] and I underline in the module.py and not in file_main.py because that I managed to do but I don't need it.
In simple terms, is it possible to change the contents of the array to modulo.py from input given by file_main.py?
it's possible to do it?
I'm sorry maybe if the question is trivial I'm a neophyte and I still have some difficulties on some steps.
In Python each code file is a "module". Modules each have a namespace. You can access these variables if you have access to their namespace. When you do "import module" you are gaining access to the module's namespace.

I have three files;
test1.py:
x = 10

def printx():
    print(x)
test2.py
import test1

def printx():
    print(test1.x)

def setx(value):
    test1.x = value
test3.py
import math
import test1
import test2
from test1 import x

print(x, test1.x)
test1.printx()
test2.printx()

print('test2.setx(42)')
test2.setx(42)
print(x, test1.x)
test1.printx()
test2.printx()

print('test1.x = PI')
test1.x = 'PI'
print(x, test1.x)
test1.printx()
test2.printx()

print('x = math.pi')
test1.x = math.pi
print(x, test1.x)
test1.printx()
test2.printx()
When I execute test3.py I get this:
Output:
========== RESTART: C:\Users\hystadd\Documents\python\sandbox\junk.py ========== 10 10 10 10 test2.setx(42) 10 42 42 42 test1.x = PI 10 PI PI PI x = math.pi 10 3.141592653589793 3.141592653589793 3.141592653589793
Notice that I can set the value of test1.py from modules test1, test2 or test3. When I change the value of test1.x the change can be seen in test1, test2 and test3.

Also notice that "from test1 import x' does not work the same as explicitly using the namespace (test1.x). It would appear that "from test1 import x" creates a new variable in the current module's namespace. This was verified by checking the variable id.
Output:
>>> id(x) 140721929705408 >>> id(test1.x) 2714390832848
This could be an issue if you use from..import.. and expect to be able to change an immutable variable.
Thank you for the example, and I admit that I made a stupid mistake complicating my life by creating an array variable, in short, I widened the circle then using the .tolist () etc. etc.
Starting with your example, my question is even simpler.
is it possible to change the variable X in Test1.py through Test3.py?
that is X = 10 in Test1.py
when I run Test3.py I import the variable X, which after some calculations becomes a value = 15, so this new x = 15, can I overwrite it at X = 10 you take in Test1.py?

Example:

test1.py
x=10


------------------------------------------------------------------------
test3.py
from test1 import x

y = int(x + 5)
print (y) ## result 15


-------------------------------------------------------------------------
(new)test1.py
x = 15 ## new value
Yes, you can modify variables in other modules during script execution. You just have to import it, index the variable you want to change, and then assign that variable a new value.

File testA.py:
x = 1
File testB.py:
import testA

print(testA.x)
testA.x = 200
print(testA.x)
When executing testA.py:
Output:
1 200
is not what I mean because so I modify the x in TestB but in TestA it always remains equal to 1 because I create a TestC and import the Testa.x would read 1.
I try to be clearer

I create a FILE X module with X = 10,
then I create three independent files
file1.py
file2.py
file3.py
in which I import FILEX (from FILEX import x)
now in each of the three (File1, File2, File3) I put an IF condition

if FILEX.x >= 10:
y = x + 1
FILEX.x = FILEX.x + y

now I would like the value of Y, which is equal to 11, to literally replace the value 10 inside the FILE X module, so that when FIle2.py calls the x of FILE X the x is equal to 11 and no longer 10.
I don't need to call it from File1.py I need to call it from FILEX
You can replace the contents of a file. And since a module is just a file, there is nothing preventing you from replacing code in a module. However, doing so is a REALLY BAD IDEA in almost all circumstances.

What you are trying to do is make a file for saving and restoring values, a database. Python has tools for handling databases from sqlite for a full featured database with sophisticated querying support to JSON, HTML or CSV files that can write values to a file and read them from a file.
update:
solved, I was using the function for the old version of python



in reality the famous variable X that must be updated would be a "wallet" variable, that is, if the test1.py performs an operation and "spends" money the wallet is empty so file2 and file3 cannot buy until the wallet is full again, let's say my idea was a common wallet for three separate scripts.
but I partially solved the problem, but a further problem arises and question, is it possible to import the module again?
in the sense, I import a module, this is modified by the code, can I re-import the module so that it is updated?
is a sort of "reload" of the imported module possible?
Pages: 1 2