Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python多线程运行无限循环_Python_Python Multithreading - Fatal编程技术网

使用python多线程运行无限循环

使用python多线程运行无限循环,python,python-multithreading,Python,Python Multithreading,所以我有一个类似的问题: 我想创建一些线程(最多50个),它们同时运行相同的代码,具有无限的while循环。这些线程之间没有交互。这背后的实际想法是,我有一系列WS2811 LED,我想用不同的颜色模式(如闪烁)独立控制它们 类似问题的问题是,如果每个线程都执行相同的操作,我不想为每个线程创建50个类。我想基于一个公共类创建这些线程,并使用for循环。我遇到的问题是,这个无限循环中只有一个线程,而另一个线程甚至没有启动。我该如何解决这个问题 import threading import tim

所以我有一个类似的问题:

我想创建一些线程(最多50个),它们同时运行相同的代码,具有无限的while循环。这些线程之间没有交互。这背后的实际想法是,我有一系列WS2811 LED,我想用不同的颜色模式(如闪烁)独立控制它们

类似问题的问题是,如果每个线程都执行相同的操作,我不想为每个线程创建50个类。我想基于一个公共类创建这些线程,并使用for循环。我遇到的问题是,这个无限循环中只有一个线程,而另一个线程甚至没有启动。我该如何解决这个问题

import threading
import time


class LEDManager(threading.Thread):
    def __init__(self, id_manager):
        threading.Thread.__init__(self)
        self.id_manager = int(id_manager)

    def initiate(id_manager):
        while True:
            print("Thread " + str(id_manager) + " blink on")
            time.sleep(2)
            print("Thread " + str(id_manager) + " blink off")
            time.sleep(2)


def main():
    thread_id = ("0", "1")
    led_index = 0
    thread_list = list()
    for objs in thread_id:
        thread = threading.Thread(target=LEDManager.initiate(led_index), args=(led_index,))
        thread_list.append(thread)
        time.sleep(1)
        led_index += 1
    for thread in thread_list:
        thread.start()


if __name__ == "__main__":
    main()
上述代码的输出为:

Thread 0 blink on
Thread 0 blink off
Thread 0 blink on
Thread 0 blink off
.
.
.

这里有一种方法可以重构代码使其工作

import threading
import time


class LEDManager(object):
    def __init__(self):
        pass

    def initiate(self, idx):
        while True:
            print("Thread " + str(idx) + " blink on")
            time.sleep(2)
            print("Thread " + str(idx) + " blink off")
            time.sleep(2)


def main():
    thread_list = list()
    l = LEDManager()
    for i in range(50):
        thread = threading.Thread(target=l.initiate, args=(i,))
        thread_list.append(thread)

    for thread in thread_list:
        thread.start()
当然,它可以用更好的实践方式编写,我的建议是


请记住,GIL不会为您提供真正的线程行为(真正的并行运行)。您可以查看一下这一点,因为您是从
线程派生
LEDManager
。线程
,它是一个线程。不要创建新的
threadingThread
对象来运行其成员函数!只需创建
LEDManager
start()
的实例即可:

import threading
import time


class LEDManager(threading.Thread):
    def __init__(self, id_manager):
        threading.Thread.__init__(self)
        self.id_manager = int(id_manager)

    def run(self):
        while True:
            print("Thread " + str(self.id_manager) + " blink on")
            time.sleep(2)
            print("Thread " + str(self.id_manager) + " blink off")
            time.sleep(2)


def main():
    thread_id = ("0", "1")
    led_index = 0
    thread_list = list()
    for objs in thread_id:
        thread = LEDManager(led_index)
        thread_list.append(thread)
        led_index += 1
    for thread in thread_list:
        thread.start()
        time.sleep(1)


if __name__ == "__main__":
    main()
(贷记@stovfl)

当线程为
start()
ed时,将自动调用


我还将1秒睡眠移到了开始循环,以实现线程输出的均匀交错,我怀疑这是您想要的。

将整行
threading.Thread(target=…
更改为:
LEDManager(led\u index)
并重命名
def initiate(id\u manager):
def run(self):