使用python进行线程化,没有错误,但程序似乎无法正常工作

我正在为树莓派开发一个程序。目前,我只是想让程序打印“黄色LED亮”或“红色LED亮”。我对线程是全新的,但是对python却不是。我目前有一些代码写出来,但是我不知道为什么当我运行它时,什么也没有发生,并且程序永远不会终止。如果有人有任何想法,将不胜感激。 我已经在下面发布了代码。 预先感谢。

from threading import Thread
import random
import time

def blinking_LED(n,t):
    i = 10
    for x in range (n,i):
        if (x % 2) == 0:
            color = "yellow"
            n += 1
        else:
            color = "red"
            n += 1
    # output result
        if (color == "yellow"):
            print ("Yellow LED on")
            time.sleep(2)
        else:
            print ("Red LED on")
            time.sleep(2)
    #exit function,terminate program
    s.quit()

if __name__ == "__main__":

    # value to pass into the functions
    v = 1

    # create threads
    t1 = Thread(target=blinking_LED,args=(v,1))

    # Start threads
    t1.start()

    #tell main thread to stay alive until both threads are done
    t1.join()
lbwaaa 回答:使用python进行线程化,没有错误,但程序似乎无法正常工作

top10 = top10.index[top10 == True]

无论它是什么,都不必调用s.quit(),当函数返回时,线程将终止。

我不确定您的问题在哪里,坦率地说,您的代码很难阅读。看上面的例子,它每秒打印一次“ LED on”,交替显示颜色,并在10次迭代后终止。

,

我想通了,首先我需要将“ s.quit()”更改为“ exit()”。其次,我不确定为什么,但是我将所有代码复制并粘贴到一个新文件中并尝试运行它,并且一切正常。我不确定问题是什么,但由于某种原因,它现在可以运行。 我感谢所有帮助!

本文链接:https://www.f2er.com/3114717.html

大家都在问