using interval in python

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

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
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:

Code: Alles auswählen

    while True:
        print 'do something'
        sleep(0.2)
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

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
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

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).
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

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
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

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.
Zuletzt geändert von Leonidas am Samstag 2. Dezember 2006, 20:05, insgesamt 1-mal geändert.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

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()
sape
User
Beiträge: 1157
Registriert: Sonntag 3. September 2006, 12:52

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
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

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.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

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
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

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.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

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.
Python 47
User
Beiträge: 574
Registriert: Samstag 17. September 2005, 21:04

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:

Code: Alles auswählen

import cd
mfg

Thomas :-)
Antworten