Using Timers and Threads in Python to create assistive command line application


Let me describe my scenario:

I am developing a command line-based proof-of-concept for the instructional system, and I am writing it in Python. System works on the principles of behavioral psychology. I am rewarding users (children with special needs) for correct answers, and I am giving them correcting consequences for the wrong answers. I am also needing to provide hints to answers if they are struggling with answers.

Here is a high level pseudo code for my application:

<blockquote> <h2>Trial:</h2> <ol><li>Ask question such as 'What color is grass?' </li> <li>

wait for answer<br /> a. If answer not provided within n seconds issue helping hint (e.g. "Gr, Gree")<br /> b. If answer is not provided within n+m seconds issue a corrective consequence. (e.g.,"Color of the grass is green.Let's try it again. ") and repeat the process<br /> c. If correct answer is provided during any n or n+m time praise the student and repeat the process.<br /> d. If the incorrect answer is provided during any of the n+m time, issue a corrective consequence and repeat the trial.

</li> <li>

Conclude the trial

</li> </ol></blockquote>

So here is my observation and a dilemma:

Once I issue a question to student, I am waiting for events to happen on their own, or for my waiting to time out. I am also supposed to issue a hint during the wait period.

So far, in my main flow I used two threading.Timer objects, one to issue hint after n seconds and another one to issue corrective consequence if the answer never happens during the n+m period.

If answer happens of any kind during n or n+m time, I cancel the timers.

My question, however, is related to the main process. I also want to cancel the waiting on the answer if the answer waiting time-out happen. Just as I cancel timers, I also want to cancel waiting on the command line for the input if my final no-answer timeout occurs.

I am thinking to have one threading.Thread (get_answer) and two Timers (provide_hint, timeout_wait).

Thread get_answer is waiting on a response from a command line.

First timer (provide_hint) is giving a hint to user if there is no answer after n seconds.

Second timer (timeout_wait) is canceling out the thread (get_answer) if no answer happens after some period of n+m seconds.

Thread get_answer can cancel both timers if the answer happens be it correct or incorrect.

<strong>Questions I have here are:</strong>

A) Are my concurrent data structures correctly used and do you have any other suggestions?

B) Are there any possible deadlock issues and how to avoid them?

Thanks in advance.


Here is a solution without using threads. It was tested on Linux, but probably works everywhere (but I'm unsure about Windows). It arranges for a SIGALRM signal to be delivered.

import signal class Alarm(Exception): pass def stop_me(*args): raise Alarm signal.signal(signal.SIGALRM, stop_me) def raw_input_with_timeout(timeout): try: signal.alarm(timeout) # number in seconds try: return raw_input() finally: signal.alarm(0) # stop the alarm except Alarm: return "alarm!" # or anything

It's not a final solution because there are issues: if the user already typed part of a line, a SIGALRM will interrupt it, but what he typed so far remains in the read buffer of the terminal. It will be part of what the next raw_input() returns. You may have to turn the terminal in raw mode to avoid that. See <a href="https://stackoverflow.com/questions/13104460/c-confusion-about-raw-vs-cooked-terminal-modes" rel="nofollow">Confusion about raw vs. cooked terminal modes?</a> .


  • Problem with memory leak check tools in Linux [closed]
  • Qt version in centos7
  • Where should I put testcode for an Eclipse plugin fragment?
  • Redirect website to mobile site and from mobile to desktop?
  • How to use data from Mongo and PostgreSQL as in-memory lookup tables?
  • How to convert text file automatically to graphviz dot file?
  • How do I force a browser window to always be on top and in focus
  • How WinForm and uwp communicate in two directions
  • Permit array in params
  • I am looking for an algorithm that calculates the power of a number. (x^y), x and y are integers . I
  • Get attributes of existing SVG elements and bind as data with d3.js
  • How to emulate integrated numeric keypad cursor keys in linux
  • Setting color in a row of a Jtable
  • HSQLDB Statement and Java NaN doubles
  • Is C++ compilable with OpenMP and boost on MacOS?
  • UIButton Borders Function Only Gives Back White Borders
  • How to change placeholder text in an autocomplete activity of android google place?
  • What is this error in fortran and how to stop the program when it occurs?
  • pandas computation in each group
  • Does CSS support inheritance [duplicate]
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Cut the background to expose the layer below
  • Floated image with variable width and heading with background image
  • Java color detection
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Chart.js Multiple dataset
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Google Custom Search with transparent background
  • Android fill_parent issue
  • Highlight and Bold text in JTextPane
  • Change multiple background-images with jQuery
  • Algorithm for a smudge tool?
  • Align navbar back button on right side
  • How reduce the height of an mschart by breaking up the y-axis
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Sending data from AppleScript to FileMaker records
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How to set my toolbar fixed while scrolling android
  • Android Google Maps API OnLocationChanged only called once