I am trying to implement a start/stop function in my code.
For simplicity lets assume I have two functions:
def setup():
global start_process
# setting up some parameters with user inputs
actual_process()
def actual_process():
global start_process, continue_process
start_process = True
while start_process:
continue_process = True
thread_1 = threading.Thread(target=start_stop)
thread_1.daemon = True
thread_1.start()
# do something infinitely
def start_stop():
global continue_process, start_process
while continue_process:
user_input = input('Press "r" to restart or "s" to stop: ')
if user_input == 's':
continue_process = False
start_process = False
print('closing program')
if user_input == 'r':
continue_process = False
start_process = False
print('Restarting')
setup()
setup()
But when I enter 's' or 'r' the function start_stop is quitting but the while loop of actual_process() keeps running without stopping. It however launches setup(). But since actual_process() doesn't stop, I can not reset the parameters.
So my question would be, how can I change my code that the while loop stops ?
I made few changes :
continue_process
and continue_processing
with ing
actual_process()
in setup()
'
in your input(...)
linebreak
statement when it stops , so there is not another undesired loopYou can try this :
import threading
def setup():
global start_process, count
count = 0
# setting up some parameters with user inputs
# and call actual_process()
actual_process()
def actual_process():
global start_process, continue_processing, count
start_process = True
while start_process:
continue_processing = True
thread_1 = threading.Thread(target=start_stop)
thread_1.daemon = True
thread_1.start()
# do something infinitely
def start_stop():
global continue_processing, start_process
while continue_processing:
user_input = input('Press "r" to restart or "s" to stop:')
if user_input == 's':
continue_processing = False
start_process = False
print('closing program')
break
if user_input == 'r':
continue_processing = False
start_process = False
print('Restarting')
setup()
setup()
EDIT Now I do not call setup()
when restarting, the global while loop while start_process:
will do it automatically.
I also added an example_of_process()
that increment a var count
and prints it, just to simulate an infinite process that we can restart or stop.
Note that you need to press "r + Enter" to restart, and "s + Enter" to stop.
import threading
import time
def setup():
global start_process
actual_process()
def actual_process():
global start_process , continue_process, count
start_process = True
count = 0
while start_process :
continue_process = True
thread_1 = threading.Thread(target=start_stop)
thread_1.daemon = True
thread_1.start()
example_of_process() # do something infinitely
def start_stop():
global continue_process, start_process , count
while continue_process:
user_input = input('Press "r" + Enter to restart or "s" + Enter to stop: \n')
if user_input == 's':
continue_process = False
start_process = False
print("closing program")
if user_input == 'r':
continue_process = False
count = 0
print("Restarting")
def example_of_process():
global continue_process, count
while continue_process:
print("count", count)
count += 1
time.sleep(1)
setup()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.