2016-04-20 75 views
2

我有一些python的多線程問題: 生成一個線程,它獲取一些參數,如線程名稱,計數器等。在線程的「運行」部分,它調用了一些子函數(並且在那裏還有一些子函數)。但是,自變量(類)似乎不存在於子函數中:引用self.name顯示出一些錯誤(NameError:全局名稱'self'未定義爲 )。沒有任何方法可以在沒有(!!)參數化所有內容的情況下獲取這些子函數中的完整結構的內容(這將在深度4時變得很長)。希望這個簡短的例子會更好地解釋它,在SUB1第二打印線試圖訪問self.counterpython:multithreading:self作爲全局變量

#!/usr/bin/python 

import threading 
import time 

globalVar = 1; 

def sub1 (name): 
    global globalVar ; 

    print str(globalVar) + " in der 1. subfunktion von " +str(name) 
    print "teste self" + str(self.counter) + " - " + globalVar + " " +str(name) ; 
    globalVar += 1 ; 
    time.sleep(1); 
    sub2 (name) ; 
    return None ; 


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 + " mit zaehler " + str(self.counter) 
     sub1 (self.name); 

threadLock = threading.Lock() 
threads = [] ; 

# Create new threads 
count =0; 
while count < 10 : 
     count += 1; 
     threadX = myThread(count, "Thread-" + str(count), count) 
     threadX.start() 
     threads.append(threadX) 

for t in threads: 
    t.join() 
print "Exiting Main Thread" 

感謝您的幫助

+0

你有沒有嘗試在線程運行中傳遞整個對象'self'而不是隻有一個屬性'self.name'? 'SUB1(個體)' –

回答

2

,爲什麼你不只是通過self代替name

#!/usr/bin/python 

import threading 
import time 

globalVar = 1; 

def sub1 (self): 
    global globalVar ; 

    print str(globalVar) + " in der 1. subfunktion von " +str(self.name) 
    print "teste self" + str(self.counter) + " - " + str(globalVar) + " " +str(self.name) ; 
    globalVar += 1 ; 
    time.sleep(1); 
    sub2 (self.name) ; 
    return None ; 


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 + " mit zaehler " + str(self.counter) 
     sub1 (self); 

threadLock = threading.Lock() 
threads = [] ; 

# Create new threads 
count =0; 
while count < 10 : 
     count += 1; 
     threadX = myThread(count, "Thread-" + str(count), count) 
     threadX.start() 
     threads.append(threadX) 

for t in threads: 
    t.join() 
print "Exiting Main Thread" 

由於int不能string串聯,我在sub1str(globalVar)取代globalVar

作品如下:

>>> ================================ RESTART ================================ 
>>> 
Starting Thread-1 mit zaehler 1 
1 in der 1. subfunktion von Thread-1 
teste self1 - 1 Thread-1 
Starting Thread-2 mit zaehler 2 
2 in der 1. subfunktion von Thread-2 
teste self2 - 2 Thread-2Starting Thread-3 mit zaehler 3 
Starting Thread-5 mit zaehler 5 
Starting Thread-6 mit zaehler 6Starting Thread-7 mit zaehler 7Starting Thread-4 mit zaehler 4Starting Thread-8 mit zaehler 8Starting Thread-9 mit zaehler 9 
3 in der 1. subfunktion von Thread-3 




3 in der 1. subfunktion von Thread-5Starting Thread-10 mit zaehler 10 
3 in der 1. subfunktion von Thread-63 in der 1. subfunktion von Thread-73 in der 1. subfunktion von Thread-9 
teste self3 - 3 Thread-3 
3 in der 1. subfunktion von Thread-43 in der 1. subfunktion von Thread-8 


teste self5 - 3 Thread-5 
teste self7 - 3 Thread-7 

3 in der 1. subfunktion von Thread-10teste self6 - 3 Thread-6teste self9 - 3 Thread-9 
teste self4 - 4 Thread-4 
teste self8 - 5 Thread-8 

teste self10 - 6 Thread-10 


//+ A bunch of Sub2 is not defined errors ... 
0

SUB1不是 「子功能」。 Python具有「嵌套」功能,但這不是一個例子。 「sub1」有一個完全不同的範圍,並且在你的班級下看不到任何變量。

如果你想訪問類的實例(個體經營)變量和方法,你應該通過「自我」作爲參數SUB1,不self.name這是唯一字符串「Thread-#」

def sub1(thread_instance): 
    print thread_instance.name, thread_instance.threadID 

然後在「運行」方式,具有自叫它:

def run(self): 
    sub1(self) 

不要使用全局變量線程存儲從線程中的對象,因爲他們這肯定會引起頭痛,當多個線程讀取和寫入該變量。

此外,如果它們都是同一個類,那麼爲你的類指定一個threadID和counter參數有什麼意義?此外,您的代碼示例不完整,缺少代碼以使您很難理解您嘗試使用此功能以及您嘗試執行的操作。

另一件事,你不需要在每行之後使用分號,這不是C。我將首先重點理解Python語言,並在使用線程之前先獲得一些基本知識。