प्रैक्टिस में, Timer शायद आप जो चाहते हैं उसे करने का सबसे आसान तरीका है।
इस कोड को करना होगा निम्नलिखित:
- 1 सेकंड के बाद, यह प्रिंट "ARG1 ARG2"
- 2 सेकंड के बाद, यह प्रिंट "उल्लू OWLS उल्लू"
===
from threading import Timer
def twoArgs(arg1,arg2):
print arg1
print arg2
print ""
def nArgs(*args):
for each in args:
print each
#arguments:
#how long to wait (in seconds),
#what function to call,
#what gets passed in
r = Timer(1.0, twoArgs, ("arg1","arg2"))
s = Timer(2.0, nArgs, ("OWLS","OWLS","OWLS"))
r.start()
s.start()
===
उपरोक्त कोड आपकी समस्या का समाधान करेगा।
लेकिन! वैकल्पिक तरीका है, जो मल्टीथ्रेडिंग का उपयोग नहीं करता है। यह जावास्क्रिप्ट की तरह बहुत अधिक काम करता है, जो सिंगल-थ्रेडेड है।
इस सिंगल-थ्रेड संस्करण के लिए, आपको केवल उस कार्य के साथ फ़ंक्शन और उसके तर्कों को स्टोर करना होगा, जिस समय फ़ंक्शन चलाना चाहिए।
एक बार जब आपके पास फ़ंक्शन कॉल और टाइमआउट वाला ऑब्जेक्ट होता है, तो समय-समय पर जांच करें कि फ़ंक्शन निष्पादित करने के लिए तैयार है या नहीं।
ऐसा करने का सही तरीका भविष्य में चलने वाले सभी कार्यों को संग्रहीत करने के लिए priority queue बनाना है, जैसा कि नीचे दिए गए कोड में दिखाया गया है।
बस जावास्क्रिप्ट की तरह, यह दृष्टिकोण इस बात की गारंटी नहीं देता है कि फ़ंक्शन बिल्कुल समय पर चल जाएगा। एक फ़ंक्शन जो चलाने में बहुत लंबा समय लेता है उसके बाद के कार्यों में देरी होगी। लेकिन यह गारंटी देता है कि एक फ़ंक्शन को अपने टाइमआउट से नहीं चलाएगा।
इस कोड को निम्न करेगा: यह प्रिंट "132"
3 सेकंड बाद
- 1 सेकंड के बाद, यह प्रिंट "20"
- 2 सेकंड के बाद, है, यह इस्तीफा।
===
from datetime import datetime, timedelta
import heapq
# just holds a function, its arguments, and when we want it to execute.
class TimeoutFunction:
def __init__(self, function, timeout, *args):
self.function = function
self.args = args
self.startTime = datetime.now() + timedelta(0,0,0,timeout)
def execute(self):
self.function(*self.args)
# A "todo" list for all the TimeoutFunctions we want to execute in the future
# They are sorted in the order they should be executed, thanks to heapq
class TodoList:
def __init__(self):
self.todo = []
def addToList(self, tFunction):
heapq.heappush(self.todo, (tFunction.startTime, tFunction))
def executeReadyFunctions(self):
if len(self.todo) > 0:
tFunction = heapq.heappop(self.todo)[1]
while tFunction and datetime.now() > tFunction.startTime:
#execute all the functions that are ready
tFunction.execute()
if len(self.todo) > 0:
tFunction = heapq.heappop(self.todo)[1]
else:
tFunction = None
if tFunction:
#this one's not ready yet, push it back on
heapq.heappush(self.todo, (tFunction.startTime, tFunction))
def singleArgFunction(x):
print str(x)
def multiArgFunction(x, y):
#Demonstration of passing multiple-argument functions
print str(x*y)
# Make some TimeoutFunction objects
# timeout is in milliseconds
a = TimeoutFunction(singleArgFunction, 1000, 20)
b = TimeoutFunction(multiArgFunction, 2000, *(11,12))
c = TimeoutFunction(quit, 3000, None)
todoList = TodoList()
todoList.addToList(a)
todoList.addToList(b)
todoList.addToList(c)
while True:
todoList.executeReadyFunctions()
===
अभ्यास में, आप की संभावना अधिक पर बस की जाँच की तुलना में है कि जब तक पाश में अगर अपनी टाइमआउट कार्यों जाने के लिए तैयार कर रहे हैं के लिए जा रहा है। आप उपयोगकर्ता इनपुट के लिए मतदान कर सकते हैं, कुछ हार्डवेयर को नियंत्रित कर सकते हैं, डेटा पढ़ सकते हैं, आदि
यह जावास्क्रिप्ट में 'setTimeout' नहीं करता है। जो होता है वह निष्पादन के लिए फ़ंक्शन को कतारबद्ध करना है (यानी टाइमआउट समाप्त होने के बाद)। –
हाँ ... आप सही हैं। लेकिन मैं सिर्फ यही चाहता था कि मैं जो चाहता था उसका विचार देना चाहता हूं मुझे पूरी तरह से तकनीकी रूप से सही होने की आवश्यकता नहीं मिली। – brunoais
सर्वश्रेष्ठ आप थ्रेड के साथ कुछ नकली कर सकते हैं। –