473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

asynchronous alarm

Goal: turn off an audible alarm without
terminating the program. For example,
suppose a console program is running::

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?

I realize this must be a standard problem so that there
is a good standard answer. Here are some naive solutions
that occured to me.

Solution C1 (console): poll keyboard inside the loop.
E.g., <URL:http://effbot.org/librarybook/msvcrt.htm>
Problem: no platform independent way to do this?

Solution C2 (console): handle KeyboardInterrupt.
An ugly hack. But works fine.

Solution C3 (console): start alarm in one thread
and wait for raw_input. (Should that be in another
thread? It does not seem to matter.)
This seems plausible, but I know nothing about threads
except that nonprogrammers tend to make mistakes
with them, so I hesitate.

Solution G1 (gui): start alarm in a thread but
include a test for a variable that can be set
by a button push? (Sounds plausible etc.)

Solution G2 (gui): start alarm but
somehow let Tkinter listen for an event
without programming any threads. Possible??

Thanks,
Alan
Feb 24 '08 #1
2 1419
Alan Isaac <ai****@american.eduwrites:
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.
Feb 24 '08 #2
Paul Rubin wrote:
a = Event()
Thread(target=f, args=(a,)).start()
raw_input('hit return when done: ')
a.set()
Simple and elegant.
Thank you.
Alan
Feb 24 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bill H | last post by:
I have a typical username/password check against mysql type logon that Zone Alarm Pro seem to break... I haven't bought or troubleshooted this yet, I'm hoping this is a know issue with a simple...
0
by: Ishwar Rattan | last post by:
System is Mandrale 9.1 Linux with Pyhton-2.2.3 I want to abort the stdin-read operation if no input is avalable. Is it possible to achieve this? Sample code is given below. -ishwar ---...
0
by: mkent | last post by:
I'm trying to use signal.alarm to stop a run-away os.system command. Can anyone exlain the following behavior? Given following the trivial program: import os import signal def...
1
by: Sergey | last post by:
How to send alarm to a thread? I can set alarm in main thread, but how then send exception to another thread to wake it if it executes too long?
7
by: Adrian Casey | last post by:
I have a multi-threaded python application which uses pexpect to connect to multiple systems concurrently. Each thread within my application is a connection to a remote system. The problem is...
3
by: ankitks | last post by:
Hi guys, is there any utility available as a protection against endless_loop() something like this: alarm.set(5); //set timeout for 5 sec endless_loop(); alarm.reset(); //reset it to 0,...
0
by: puntino | last post by:
Hi I have developed an Alarm, I haven' problem of runtime or compiletime but the application seems blocked. When I call the method AlarmExecute, it runs only the first time ( when i click onto run...
0
by: thelightkeeper | last post by:
Hi, in MSSQL, I have 2 tables naming , --------------------------------------- EquipmentID : nvarchar(50), FK, reference .EquipmentID AlarmID : nvarchar(50), PK AlarmHappenTime :...
8
by: superleochen | last post by:
Database is simliar to this: ID TIME TAGNAME VALUE 5967383 8/1/2008 1:00:24 AM I16MUDL11 ALARM 5967384 8/1/2008...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.