2013-05-16 3 views
19

मुझे नेटवर्क कोर्स में एक प्रोग्राम लिखना है जो चुनिंदा दोहराव की तरह कुछ है लेकिन एक टाइमर की आवश्यकता है। गूगल में खोज के बाद मैंने पाया कि threading.Timer मेरी मदद कर सकते हैं, मैं सिर्फ परीक्षण के लिए एक साधारण प्रोग्राम लिखा कैसे threading.Timer काम है कि इस था:थ्रेडिंग। टिमर()

import threading 

def hello(): 
    print "hello, world" 

t = threading.Timer(10.0, hello) 
t.start() 
print "Hi" 
i=10 
i=i+20 
print i 

इस कार्यक्रम को सही ढंग से चलाने के लिए। लेकिन जब मैं एक तरीका है कि जैसे पैरामीटर देने में हैलो समारोह परिभाषित करने की कोशिश:

import threading 

def hello(s): 
    print s 

h="hello world" 
t = threading.Timer(10.0, hello(h)) 
t.start() 
print "Hi" 
i=10 
i=i+20 
print i 

बाहर डाल दिया है:

hello world 
Hi 
30 
Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner 
    self.run() 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 726, in run 
    self.function(*self.args, **self.kwargs) 
TypeError: 'NoneType' object is not callable 

मैं नहीं कर सकते समझ में समस्या क्या है! क्या कोई मेरी मदद कर सकता है?

उत्तर

47

तुम बस तर्क hello लिए इस तरह, समारोह कॉल में एक अलग आइटम में लाना,

t = threading.Timer(10.0, hello, [h]) 

यह अजगर में एक आम तरीका है की जरूरत है। अन्यथा, जब आप Timer(10.0, hello(h)) का उपयोग करते हैं, तो इस फ़ंक्शन कॉल का परिणाम Timer पर भेजा जाता है, जो None है क्योंकि hello स्पष्ट वापसी नहीं करता है।

+0

बहुत बहुत धन्यवाद :) – sandra

संबंधित मुद्दे