[英]how to stop one function when the other one stops on python
i've a working function which stops when the user writes exit, but i also have another function which doesn't stop so cmd just keeps staying open and won't close like it should as the other function keeps changing the cmd text color 我有一个工作功能,当用户写出口时该功能会停止,但我还有另一个功能不会停止,因此cmd只会保持打开状态,不会像它应该那样关闭,因为其他功能会不断更改cmd文本颜色
import os,webbrowser,time,random,sys
from threading import Thread
def fun1():
Q=1
while Q==1:
print ('would you like to:')
print('1.launch an applcation')
print('2.browse the internet')
print('========================================')
bruv=input('your answer: ')
if bruv in ('1','launch','launch app','launch an application'):
print('========================================')
print('what app would you like to open?')
print('========================================')
x=input('your answer: ')
if x not in ('word'):
y=x+'.exe'
os.startfile(y)
else:
z='win'+x
os.startfile(z)
if bruv in ('2','browse the internet','browse','browse internet'):
print('========================================')
print ('where would you like to go?')
print('========================================')
p=input('your answer: ')
p='www.'+p+'.com'
webbrowser.open(p)
print('========================================')
print ('ok')
print('========================================')
print ('wanna continue?')
if input() in ('exit','no','quit','nope','close'):
print('========================================')
print ('ok! thanks for using my bot!')
print('========================================')
time.sleep(1)
Q+=1
countdown=3
while countdown>=0:
if countdown!=0:
print (countdown)
time.sleep(1)
countdown-=1
else:
sys.exit()
def fun2():
what=1
while what==1:
os.system('color E')
time.sleep(0.2)
os.system('color A')
time.sleep(0.2)
os.system('color C')
time.sleep(0.2)
os.system('color B')
time.sleep(0.2)
os.system('color D')
time.sleep(0.2)
else:
sys.exit()
t1=Thread(target=fun1)
t2=Thread(target=fun2)
t1.start()
t2.start()
In this code fun2 keeps rolling changing the color and wont close CMD also i know this code is pretty bad i'm just starting python. 在这段代码中fun2不断滚动改变颜色,并且不会关闭CMD,我也知道这段代码很糟糕,我只是在启动python。
So the problem here is that your whole script (which starts both of your functions/threads) will on default only end when all the threads it started have stopped. 因此,这里的问题是整个脚本(同时启动两个函数/线程)在默认情况下仅在其启动的所有线程都停止时才结束。 So all your functions need to return at some point, but your
fun2()
will never finish as the endless loop while what==1
will always run as what
is always equal to 1
. 因此,所有的功能都需要在某些时候回来,但你
fun2()
将永远不会结束的死循环while what==1
将始终运行的what
总是等于1
。 (As a side note, the convention for endless loops in Python is to use while True: ...
). (作为附带说明,Python中无穷循环的约定是
while True: ...
)。
To tell a Thread that it should finish when all other threads have, you have to make it a Daemon
. 要告诉一个线程所有其他线程都应该完成时,您必须使其成为
Daemon
。 You might want to look here , where the documentation says: 您可能想在这里看到文档说明:
A thread can be flagged as a “daemon thread”.
线程可以标记为“守护程序线程”。 The significance of this flag is that the entire Python program exits when only daemon threads are left.
该标志的重要性在于,仅保留守护程序线程时,整个Python程序都会退出。 The initial value is inherited from the creating thread.
初始值是从创建线程继承的。 The flag can be set through the daemon property or the daemon constructor argument.
可以通过daemon属性或daemon构造函数参数设置该标志。
So in your example above, making t2
a deamon should work. 因此,在上面的示例中,使
t2
成为守护进程应该可以工作。
...
t1 = Thread(target=fun1)
t2 = Thread(target=fun2)
t2.deamon = True
t1.start()
t2.start()
PS: Also be aware that color
does not work in all terminals and operating systems (eg MacOS) PS:另外请注意,
color
并非在所有终端和操作系统(例如MacOS)上均有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.