2016-04-24 127 views
0

我是新的线程,甚至更新的python我经历了多个问题,出于某种原因,我无法正确理解它。多线程暂停第二个线程

我有一个显示LED(数字)状态(真/假)的应用程序,现在我希望它闪烁,因此它打开等待2秒钟,然后关闭等待2秒钟。我会在每次更改后显示它所处的状态。我创建的一个LED具有2秒一个5闪烁之间的延迟2,所以我预测这两个的输出会是这样:

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) 
+0

你不是真正创建线程,所以一切运行顺序 –

回答

1

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

下面是使用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 

here报价:

这里是一个使用Thread子类的样本(如你所说d吧):

import threading 
import time 

exitFlag = 0 

class myThread (threading.Thread): 
    def __init__(self, threadID, name, counter): 
     threading.Thread.__init__(self) 
     self.threadID = threadID 
     self.name = name 
     self.counter = counter 
    def run(self): 
     print "Starting " + self.name 
     print_time(self.name, self.counter, 5) 
     print "Exiting " + self.name 

def print_time(threadName, delay, counter): 
    while counter: 
     if exitFlag: 
      threadName.exit() 
     time.sleep(delay) 
     print "%s: %s" % (threadName, time.ctime(time.time())) 
     counter -= 1 

# Create new threads 
thread1 = myThread(1, "Thread-1", 1) 
thread2 = myThread(2, "Thread-2", 2) 

# Start new Threads 
thread1.start() 
thread2.start() 

print "Exiting Main Thread" 

在于按如下工作:

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 
+0

亲爱的向下选民,请留言让我知道什么是错的:) – EbraHim

2

您从Thread派生您的控制器,但您根本没有使用线程方法。

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

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

另请documentation

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

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