为什么我的所有主题都以主要形式返回?

时间:2013-11-16 03:10:57

标签: python multithreading python-2.5

此脚本打开和关闭2 Phidg​​ets中继(www.phidgets.com)。我想在单独的线程中打开和关闭继电器。下面的脚本运行但是将每个线程打印为主线程,如下所示

    Waiting for attach....
    172760 attached!
    Phidget InterfaceKit 8/8/8 model
    MainThread Starting
    relay state is True
    MainThread Exiting
    MainThread Starting
    relay state is True
    MainThread Exiting
    MainThread Starting
    relay state is False
    MainThread Exiting
    MainThread Starting
    relay state is False
    MainThread Exiting

谁能告诉我我做错了什么?提前谢谢。

#!/usr/bin/env python

#Basic imports
from ctypes import *
import sys, random, time, decimal, threading


#Phidget specific imports
from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
from Phidgets.Devices.InterfaceKit import InterfaceKit

class fidget(object):
#Common base class for all phidgets

    def __init__(self, device):
        self.device = InterfaceKit()

########################################        

    # open the interfacekit         
    def openIfkit(self):
        try:
            self.device.openPhidget()
            self.device.waitForAttach(10000)
            print("Waiting for attach....")
            print ("%d attached!" % (self.device.getSerialNum()))
            print ("%s model" % (self.device.getDeviceName()))
        except PhidgetException, e:
            print ("Phidget Exception %i: %s" % (e.code, e.detail))     
            exit(1)

    # open the interfacekit         
    def closeIfkit(self):
        try:
            self.device.closePhidget()
        except PhidgetException, e:
            print ("Phidget Exception %i: %s" % (e.code, e.detail))     
            exit(1)

    def relayOn(self, output):

        # Set digital output port 0 to be on
        print threading.currentThread().getName(), 'Starting'
        self.device.setOutputState(output, 1)
        time.sleep(.1)
        print 'relay state is %s' %self.device.getOutputState(output)
        print threading.currentThread().getName(), 'Exiting'

    def relayOff(self, output):
        print threading.currentThread().getName(), 'Starting'
        self.device.setOutputState(output, 0)
        time.sleep(.1)
        print 'relay state is %s' %self.device.getOutputState(output)
        print threading.currentThread().getName(), 'Exiting'

#"This would create first object of fidgit class"
x = fidget('ifkit')
x.openIfkit()


#t1 = threading.Thread( target=x.relayOn(0))
#t2 = threading.Thread(target=x.relayOn(1))
#t3 = threading.Thread(target=x.relayOff(0))
#t4 = threading.Thread(target=x.relayOff(1))

t1 = threading.Thread(target=x.relayOn, args=(0,))
t2 = threading.Thread(target=x.relayOff, args=(0,))
t3 = threading.Thread(target=x.relayOn, args=(1,))
t4 = threading.Thread(target=x.relayOff, args=(1,))

t1.start()
t2.start()
t3.start()
t4.start()

x.closeIfkit()

2 个答案:

答案 0 :(得分:2)

你需要将一个callable传递给target=,如下所示:

t1 = threading.Thread(target=lambda: x.relayOff(1))

目前,您所做的是在主线程上调用x.relay*并传递其返回值(即None,因为在未明确返回的python函数中,返回{{1} })到None

答案 1 :(得分:0)

你在主线程中每次调用relayOnrelayOff两次,而从不在你的子线程中,它们根本不做任何事情。您向target提供的threading.ThreadrelayOnrelayOff返回值,因为这些函数不会返回<)有None,因此线程不运行任何东西。

解决此问题的一种方法是创建一个可以执行所需操作的闭包:

t1 = threading.Thread( target=lambda: x.relayOn(0) )