简体   繁体   English

当另一个函数在python上停止时如何停止一个函数

[英]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.

相关问题 Python:Tkinter-一个动画在另一个开始时停止 - Python: Tkinter - One animation stops when the other begins PyTelegramBotApi - 如果其他 function 正在运行,如何停止一个 function? - PyTelegramBotApi - how to stop one function if other function is running? 当一个 function 结束时,另一个也以 Python 中的多处理结束 - When one function ends the other one also ends in multiprocessing in Python 如何停止 python 以完成一个操作的执行然后启动其他进程 - How to STOP python to complete execution on one operation and then start other process 如何在一个文件python脚本中为其他函数分配函数? - How to assign function to other function in one file python script? 如何通过Python在其他函数中使用参数调用一个函数? - How to call one function with parameter in other function by Python? 如何在 python 中启动一个线程池并在一个线程池完成时停止? - How to start a pool of threads and stop when one is finished in python? Python:如何在其他函数中使用来自一个函数的命名变量 - Python: How to use named variables from one function in other functions 当一个在Python中互补时,如何使用2个Spotify端点? - How to use 2 spotify endpoints, when one complement the other in Python? 如何仅导入模块的一部分并停止在Python中编译其他部分? - How to import only one part of a module and stop compiling other parts in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM