Seite 1 von 1
using interval in python
Verfasst: Mittwoch 29. November 2006, 22:51
von reverse
Hi all
How are you doing?
My first post here and please pardon for my language...hope you dont mind?
I am into Actionscripting but slowly learning python and it`s fun!
my first question.
How to call a function using timer/interval in python?
I need to call a function over and over again using timer inbetween.
for example in actionscripting:
Code: Alles auswählen
var intervalId:Number;
var count:Number = 0;
var maxCount:Number = 10;
var duration:Number = 2000;
function executeCallback():Void {
trace("executeCallback intervalId: " + intervalId + " count: " + count);
if(count >= maxCount) {
clearInterval(intervalId);
}
count++;
}
intervalId = setInterval(this, "executeCallback", duration);
or simply call a function with time gap for infinty of time?
regards
reverse
Verfasst: Mittwoch 29. November 2006, 23:55
von BlackJack
If you want the function to be called asynchronous the you need a thread:
Code: Alles auswählen
from time import sleep
from threading import Thread
class Counter(Thread):
def __init__(self, duration, max_count):
Thread.__init__(self)
self.duration = duration
self.max_count = max_count
def run(self):
for count in xrange(self.max_count):
print 'counter %s: %d' % (self.getName(), count)
sleep(self.duration)
def main():
counter = Counter(0.2, 10)
counter.start()
If you just want to call a function in the main thread over and over again it's just:
Verfasst: Samstag 2. Dezember 2006, 17:33
von reverse
Hi BlackJack
Thanks for the reply!
I am quite newbie to use class function in python, therefore I tried this simple eject cdrom in and out but didn`t work.
Code: Alles auswählen
from time import sleep
import WinCDRom
summe = 0
while summe < 50:
print "repeat"
cd = WinCDRom.Cdrom()
cd.eject()
cd.close()
cd.load()
sleep(0.2)
things which are not clear to me:
1/ sleep(0.2)....does it mean pause for 0.2 milli sec and repeat over?
what I missing here is something..like updateAfterEvent.
best regards
reverse
Verfasst: Samstag 2. Dezember 2006, 17:50
von Leonidas
reverse hat geschrieben:I tried this simple eject cdrom in and out but didn`t work.
Thats because you forgot to increment `summe` in your `while`-Loop
reverse hat geschrieben:sleep(0.2)....does it mean pause for 0.2 milli sec and repeat over?
This means pause for 0.2 seconds, that's 200 milliseconds, but yes, it repeats every 1/5 second (provided that eject() close() and load() return immediately).
Verfasst: Samstag 2. Dezember 2006, 18:43
von reverse
Thats because you forgot to increment `summe` in your `while`-Loop
where do I insert the increment??
example 1
Code: Alles auswählen
from time import sleep
import WinCDRom
summe = 0
while summe++ < 50:
print "repeat"
cd = WinCDRom.Cdrom()
cd.eject()
cd.close()
cd.load()
sleep(0.2)
a simple example from AS
Code: Alles auswählen
var i:Number = 0;
while (i++ < 5) {
trace("this is execution " + i);
}
I am not quite familiar with python operators but when I compare the both code AS is seems to me right now more logical.
am I missing something?
thanks and regards
reverse
Verfasst: Samstag 2. Dezember 2006, 18:57
von Leonidas
Try this:
Code: Alles auswählen
import time
import WinCDRom
summe = 0
while summe < 50:
print "repeat"
cd = WinCDRom.Cdrom()
cd.eject()
cd.close()
cd.load()
summe += 1
time.sleep(0.2)
Edit: Corrected namespace issue.
Verfasst: Samstag 2. Dezember 2006, 18:59
von reverse
get following error
Traceback (most recent call last):
File "loopOvertime3.py", line 2, in ?
import WinCDRom
ImportError: No module named WinCDRom
but following code works
Code: Alles auswählen
import WinCDRom
cd = WinCDRom.Cdrom()
cd.eject()
cd.close()
cd.load()
Verfasst: Samstag 2. Dezember 2006, 19:46
von sape
Make this:
Code: Alles auswählen
import time
import WinCDRom
summe = 0
cd = WinCDRom.Cdrom()
while summe < 50:
print "repeat"
cd.eject()
cd.close()
cd.load()
summe += 1
sleep(0.2)
that should be able be done (not tested).
Sry, for my bad english :/
lg
Verfasst: Samstag 2. Dezember 2006, 20:06
von Leonidas
reverse hat geschrieben:get following error
That's impossible: I guess you are starting both codes from different directories an in one of there your WinCDRom module cannot be found. You have to start both scripts from the same directory.
Verfasst: Sonntag 3. Dezember 2006, 19:10
von reverse
Hi all
thanks for yours reply!
I am still unable to solve the cd.eject and close
Leonidas (I am not sure, what you mean by running codes from different directories??)
I am using scite to edit...and importing both module
at least the below part works.
Code: Alles auswählen
from time import sleep
import winsound
#import WinCDRom
summe = 0
#cd = WinCDRom.Cdrom()
while summe < 5:
print "repeat"
# Play Windows exit sound.methods (SystemQuestion), (SystemExclamation), (SystemExit), (SystemHand), (SystemQuestion)
winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)
# cd.eject()
# cd.close()
# cd.load()
summe += 1
sleep(1.2)
thanks
reverse
Verfasst: Sonntag 3. Dezember 2006, 19:51
von Leonidas
reverse hat geschrieben:Leonidas (I am not sure, what you mean by running codes from different directories??)
Where is this WInCDRom module from? It is not an integral part of the Python distribution from python.org.
Verfasst: Sonntag 3. Dezember 2006, 20:30
von reverse
from following tutorial
http://aspn.activestate.com/ASPN/Cookbo ... ipe/180919
it says:
Using the Python Win32 Extensions, the module automatically detects all cd drives and defaults to the first drive found or to a programmer specified drive.
Verfasst: Sonntag 3. Dezember 2006, 20:43
von Python 47
Hi Reverse:
Copy the Sourcecode from the page to scite and save it to:
C:\Python2x\Lib\WinCDRom.py
Then you save this sourcecode to C:\Python2x\cd.py
Code: Alles auswählen
import time
import WinCDRom
summe = 0
while summe < 50:
print "repeat"
cd = WinCDRom.Cdrom()
cd.eject()
cd.close()
cd.load()
summe += 1
time.sleep(0.2)
Then you start the Python Interpreter and type: