asynchronous alarm

Paul Rubin http
Sun Feb 24 02:19:21 EST 2008


Alan Isaac <aisaac at american.edu> writes:
>         while True:
>                 sys.stdout.write('\a')
>                 sys.stdout.flush()
>                 time.sleep(0.5)
> 
> I want to add code to allow me to turn off this alarm and then
> interact with the program in its new state (which the alarm alerts
> me to).
> 
> Question: how best to do this mostly simply in a console application
> and in a Tkinter application?

You'd usually use another thread to tell the loop when to exit,
or run the loop itself in another thread:

    import sys,time
    from threading import Event, Thread

    def f(event):
       while not event.isSet():
           sys.stdout.write('\a')
           sys.stdout.flush()
           time.sleep(0.5)

    a = Event()
    Thread(target=f, args=(a,)).start()
    raw_input('hit return when done: ')
    a.set()

see the docs for the threading module, to make sense of this.



More information about the Python-list mailing list