[PIPython] Eseguire una operazione a intervalli di tempo.

Nicola Paolucci nick
Ven 19 Nov 2004 15:16:44 CET


ciao Giovanni,

Giovanni B. Lenoci wrote:

> Ciao, vorrei prendere dei valori dai files presenti nei file contenuti 
> nella dir /proc/ a intervalli di tempo.
> Lasciando stare il chron di sistema mi chiedevo qual'era il metodo 
> migliore per eseguire un operazione a intervalli regolari.
>
> La prima prova che ho fatto č stato memorizzare il valore del time 
> all'inizio dello script.
>
> inizio=time.time()
>
> e poi ho incluso il tutto in un while infinito
>
>
> while 1:
>       print int(time.time()-inizio)
>
> cosi ottengo effetivamente i secondi che passano, ma ottengo anche la 
> barretta della CPU rossa!
> era ovvio che questo non fosse l'approccio giusto.
>
> Sapete darmi qualche dritta su quale modulo usare e come?
> Datemi anche solo uno spunto, poi cercherņ di sbrigarmela da solo :-)
>
Dai un'occhiata al modulo sched della libreria standard:

*class scheduler*( 	timefunc, delayfunc)

    The scheduler class defines a generic interface to scheduling
    events. It needs two functions to actually deal with the ``outside
    world'' -- timefunc should be callable without arguments, and return
    a number (the ``time'', in any units whatsoever). The delayfunc
    function should be callable with one argument, compatible with the
    output of timefunc, and should delay that many time units. delayfunc
    will also be called with the argument |0| after each event is run to
    allow other threads an opportunity to run in multi-threaded
    applications. 

Example:

>>> import sched, time
>>> s=sched.scheduler(time.time, time.sleep)
>>> def print_time(): print "From print_time", time.time()
...
>>> def print_some_times():
...     print time.time()
...     s.enter(5, 1, print_time, ())
...     s.enter(10, 1, print_time, ())
...     s.run()
...     print time.time()
...
>>> print_some_times()
930343690.257
>From print_time 930343695.274
>From print_time 930343700.273
930343700.276

ciao,
    Nick



More information about the Python mailing list