2010-04-29 39 views
1

我寫了python腳本來掃描wifi和發送數據到服務器,我設置了間隔值,所以它繼續掃描併發送數據,它從config.txt文件中讀取我設置掃描間隔值,我也在我的配置文件中添加是/否,所以是'不'它只會掃描一次,如果'是'它會根據間隔級別掃描,python腳本問題一旦建立和打包它

我的代碼爲下面

import time,..... 

from threading import Event, Thread 

class RepeatTimer(Thread): 
    def __init__(self, interval, function, iterations=0, args=[], kwargs={}): 
     Thread.__init__(self) 
     self.interval = interval 
     self.function = function 
     self.iterations = iterations 
     self.args = args 
     self.kwargs = kwargs 
     self.finished = Event() 

    def run(self): 
     count = 0 
     while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations): 
      self.finished.wait(self.interval) 
      if not self.finished.is_set(): 
       self.function(*self.args, **self.kwargs) 
       count += 1 

    def cancel(self): 
     self.finished.set() 

    def scanWifi(self): 
     #scanning process and sending data done here 


obj = JW() 
if status == "yes": 
    t = RepeatTimer(int(intervalTime),obj.scanWifi) 
    t.start() 
else: 
    obj.scanWifi() 

一次我包我的代碼,它唯一的奔跑,當我把我的配置文件設置爲「無」,其中它僅掃描一次,但是當我把我的配置文件爲「是」,就沒有進步根本沒有,所以我創辦d,有我的同班同學RepeatTimer(定時器)一旦建立,但不知道如何解決問題

誰能幫助我

感謝

回答

0

我認爲這個問題是在循環狀態。假設is_set()返回False,則第二部分總是False。雖然是intervalTime是未知的,我認爲它是積極的(是否感知負間隔時間?)和count永遠不會比self.iterations小:它們都是0

但是你發佈的代碼太少,它不知道如何確切的工作。

while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):