多线程暂停第二个线程

时间:2016-04-24 09:41:58

标签: python multithreading

我是线程新手,甚至更新的python我经历了多个问题,由于某种原因我无法做到正确。

我有一个应用程序显示LED(数字)状态(真/假)现在我希望它闪烁所以它打开等待2秒然后关闭等待2秒。我会在每次更改后显示它所处的状态。我已经创建了2个LED,其中一个在2秒1的闪烁之间有延迟,因此我预测这两个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)

2 个答案:

答案 0 :(得分:2)

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

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

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

另见documentation

  

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

     

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

答案 1 :(得分:1)

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

以下是使用ObservableCollection<T>模块实现多线程程序的另一种方法:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class RoomsViewModel : INotifyPropertyChanged
{
    /* constructor goes here from previous code block */

    private ObservableCollection<AvailableRoomModel> availableRooms;
    public ObservableCollection<AvailableRoomModel> AvailableRooms
    {
        get { return availableRooms; }
        set { availableRooms = value; OnPropertyChanged(); }
    }

    private ObservableCollection<AvailableRoomModel> list;
    public ObservableCollection<AvailableRoomModel> List
    {
        get { return list; }
        set { list = value; OnPropertyChanged(); }
    }

    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

如下工作:

thread

引自here

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

其工作原理如下:

Thread