Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 Multiplethreading暂停第二个线程_Python_Multithreading - Fatal编程技术网

Python Multiplethreading暂停第二个线程

Python Multiplethreading暂停第二个线程,python,multithreading,Python,Multithreading,我是线程新手,甚至是python新手。我经历了许多问题,但由于某些原因,我无法正确理解 我有一个显示LED(数字)状态(真/假)的应用程序,现在我希望它闪烁,使其打开等待2秒,然后关闭等待2秒。我显示每次更改后它处于打开状态。我已经创建了两个LED,其中一个闪烁之间有2秒1 5的延迟,因此我预测这两个LED的输出如下: LED: 15 State: True LED: 16 State: True LED: 16 State: False LED: 15 State: False 相反,我得到

我是线程新手,甚至是python新手。我经历了许多问题,但由于某些原因,我无法正确理解

我有一个显示LED(数字)状态(真/假)的应用程序,现在我希望它闪烁,使其打开等待2秒,然后关闭等待2秒。我显示每次更改后它处于打开状态。我已经创建了两个LED,其中一个闪烁之间有2秒1 5的延迟,因此我预测这两个LED的输出如下:

LED: 15 State: True
LED: 16 State: True
LED: 16 State: False
LED: 15 State: False
相反,我得到了

LED: 15 State: True
LED: 15 State: False
LED: 16 State: True
LED: 16 State: False
代码本身:

import time
from threading import Thread

class ledController(Thread):
    #static variables
    def __init__(self, GPIO, state=False): # x = " " - Default variable if user leaves it empty
        self.GPIO = GPIO
        self.state = state #Default LED state is off
    def ledSwitch(self):
        self.state = not self.state
    def ledON(self):
        self.state = True
    def ledOFF(self):
        self.state = False

    def ledBlink(self, duration):
        self.ledON()
        print(self.ledDetails())
        time.sleep(duration)
        self.ledOFF()
        print(self.ledDetails())
        time.sleep(duration)

    def ledDetails(self):
        return "LED: " + str(self.GPIO) + " State: " + str(self.state)


redLED = ledController(15)
blueLED = ledController(16, True)

redLED.ledBlink(5)
blueLED.ledBlink(2)

您正在从
线程
派生控制器,但根本不使用线程方法

因此,所有方法都是同步执行的,这就是输出的生成方式

应该在派生类中创建run()方法,然后使用.start()启动线程

另见:

run()-表示线程活动的方法

您可以在子类中重写此方法。标准run()方法调用作为目标参数(如果有)传递给对象构造函数的可调用对象,其顺序参数和关键字参数分别取自args和kwargs参数


您正在从
线程
派生控制器,但根本不使用线程方法

因此,所有方法都是同步执行的,这就是输出的生成方式

应该在派生类中创建run()方法,然后使用.start()启动线程

另见:

run()-表示线程活动的方法

您可以在子类中重写此方法。标准run()方法调用作为目标参数(如果有)传递给对象构造函数的可调用对象,其顺序参数和关键字参数分别取自args和kwargs参数


您根本没有使用多线程功能。您只是在类中派生了线程模块方法,但没有使用它们

下面是使用
thread
模块实现多线程程序的另一种方法:

import time
import thread

class ledController():
    #static variables
    def __init__(self, GPIO, state=False): # x = " " - Default variable if user leaves it empty
        self.GPIO = GPIO
        self.state = state #Default LED state is off
    def ledSwitch(self):
        self.state = not self.state
    def ledON(self):
        self.state = True
    def ledOFF(self):
        self.state = False

    def ledBlink(self, duration):
        self.ledON()
        print(self.ledDetails())
        time.sleep(duration)
        self.ledOFF()
        print(self.ledDetails())
        time.sleep(duration)

    def ledDetails(self):
        return "LED: " + str(self.GPIO) + " State: " + str(self.state) + '\n'


redLED = ledController(15)
blueLED = ledController(16, True)


try:
   thread.start_new_thread( redLED.ledBlink, (5, ) )
   thread.start_new_thread( blueLED.ledBlink, (2, ) )
except:
   print "Error: unable to start thread"
工作内容如下:

>>> ================================ RESTART ================================
>>> 
>>> LED: 15 State: True
LED: 16 State: True
LED: 16 State: False
LED: 15 State: False
Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2

引自:

下面是一个使用
线程
子类的示例(正如您所做的):

其工作原理如下:

>>> ================================ RESTART ================================
>>> 
>>> LED: 15 State: True
LED: 16 State: True
LED: 16 State: False
LED: 15 State: False
Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2

您根本没有使用多线程功能。您只是在类中派生了线程模块方法,但没有使用它们

下面是使用
thread
模块实现多线程程序的另一种方法:

import time
import thread

class ledController():
    #static variables
    def __init__(self, GPIO, state=False): # x = " " - Default variable if user leaves it empty
        self.GPIO = GPIO
        self.state = state #Default LED state is off
    def ledSwitch(self):
        self.state = not self.state
    def ledON(self):
        self.state = True
    def ledOFF(self):
        self.state = False

    def ledBlink(self, duration):
        self.ledON()
        print(self.ledDetails())
        time.sleep(duration)
        self.ledOFF()
        print(self.ledDetails())
        time.sleep(duration)

    def ledDetails(self):
        return "LED: " + str(self.GPIO) + " State: " + str(self.state) + '\n'


redLED = ledController(15)
blueLED = ledController(16, True)


try:
   thread.start_new_thread( redLED.ledBlink, (5, ) )
   thread.start_new_thread( blueLED.ledBlink, (2, ) )
except:
   print "Error: unable to start thread"
工作内容如下:

>>> ================================ RESTART ================================
>>> 
>>> LED: 15 State: True
LED: 16 State: True
LED: 16 State: False
LED: 15 State: False
Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2

引自:

下面是一个使用
线程
子类的示例(正如您所做的):

其工作原理如下:

>>> ================================ RESTART ================================
>>> 
>>> LED: 15 State: True
LED: 16 State: True
LED: 16 State: False
LED: 15 State: False
Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2

您没有实际创建线程,所以所有内容都是按顺序运行的您没有实际创建线程,所以所有内容都是按顺序运行亲爱的down voter,请留下评论让我知道有什么问题:)亲爱的down voter,请留下评论让我知道有什么问题:)