Python Forum
Stopping a loop in another file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stopping a loop in another file
#1
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!
Reply
#2
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
Reply
#3
So, the run_func is a global variable in File2? And by writing File2.run_func can acces the global variable from that File2?
Reply
#4
@Yoriz,thank you!
Reply
#5
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  stopping number conversion before end Skaperen 6 2,942 Jul-12-2020, 09:22 AM
Last Post: DeaD_EyE
  Help with Stopping a function after a timer SheeppOSU 0 1,912 Jan-28-2019, 10:13 PM
Last Post: SheeppOSU
  reading raw data until condition is met, then stopping unknowntothem 7 4,074 Sep-27-2018, 06:10 AM
Last Post: unknowntothem
  Stopping scheduler with condition j.crater 17 15,738 Jan-10-2017, 11:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020