Python Forum

Full Version: Stopping a loop in another file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

File1.py:
import File2

while True:
    a = input()
    if a == "start":
        threading.Thread(target = File2.func()).start()
    if a == "stop":
        #stop function File2.func
File2.py:
def func():
    while True:
        #do stuff
How can i stop the running of func() in File2? I have no idea! I cannot pass a stop variable to func() because the variable are one time loaded at the beggining of the func() running.
Thank you!
Maybe this will suffice

File1.py:
import File2
 
while True:
    a = input()
    if a == "start":
        File2.run_func = True
        threading.Thread(target = File2.func).start()
    if a == "stop":
        #stop function File2.func
        File2.run_func = False
File2.py:
run_func = True

def func():
    while run_func:
        #do stuff
Edit: I removed the () from File2.func as the thread should call the function
So, the run_func is a global variable in File2? And by writing File2.run_func can acces the global variable from that File2?
@Yoriz,thank you!
(Jun-13-2020, 06:12 PM)Yoriz Wrote: [ -> ]Maybe this will suffice

File1.py:
import File2
 
while True:
    a = input()
    if a == "start":
        File2.run_func = True
        threading.Thread(target = File2.func).start()
    if a == "stop":
        #stop function File2.func
        File2.run_func = False
File2.py:
run_func = True

def func():
    while run_func:
        #do stuff

Is not working, i cannot update the value of File2.run_func into False. If I do:
print(File2.run_func)
after run the line:
File2.run_func = False
the output is:
Output:
True
instead of False



EDIT: It works! Sorry, my mistake!Thank you all!