简体   繁体   English

python 中的线程程序在 IDLE 上成功运行,但在 shell 上失败,命令行

[英]Threading program in python running successfully on IDLE but not on shell, command line

I am using python 3.8 on windows 10. I am able to run threading programs succesfully on IDLE but, the same programs do not start on command line or when I double click them.我在 windows 10 上使用 python 3.8。我能够在 IDLE 上成功运行线程程序,但是,相同的程序不会在命令行上启动或当我双击它们时。 The shell pops up and exits quickly even when threads are not started.即使没有启动线程,shell 也会快速弹出并退出。 I even tried to catch any runtime errors and any errors using except but I got neither on IDLE and the program was still terminating abruptly on shell.我什至尝试使用 except 来捕获任何运行时错误和任何错误,但我在 IDLE 上都没有,并且程序仍在 shell 上突然终止。

Here is an example -这是一个例子 -

    import threading
    import time
    try:
        def func():
            for i in range(10):
                print(i)
                time.sleep(0.1)                 

        t1 = threading.Thread(target = func)

        t1.start()
        #t1.join() # i tried this also
        while t1.is_alive():
            time.sleep(0.1) #trying to return back, i added this when the threads were not working

        input() #waiting for the user to press any key before exit
    except RuntimeError:
        print('runtime error')
        input()
    except: # any error
        print('Some error')
        input()

I found that I had made a file by the name 'threading.py' in the directory.我发现我在目录中创建了一个名为“threading.py”的文件。 This file was causing Attribute Error of python because it has the same name as the 'threading' module of python.此文件导致 python 的属性错误,因为它与 python 的“线程”模块同名。 I renamed it and my programs are working jolly good!我重命名了它,我的程序运行良好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM