Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 119441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:39:13+00:00 2026-05-11T03:39:13+00:00

Maybe i cant do what i want? I want to have 1 thread do

  • 0

Maybe i cant do what i want? I want to have 1 thread do w/e it wants and a 2nd thread to recv user input to set the quit flag. using this code i want to enter q anytime to quit or have it timeout after printing hey 6 times

import sys import threading import time  class MyThread ( threading.Thread ):     def run (s):         try:             s.wantQuit = 0             while(not s.wantQuit):                 print 'want input'                 button = raw_input()                 if button == 'q':                     s.wantQuit=1         except KeyboardInterrupt:             s.wantQuit = 1             print 'abort with KeyboardInterrupt'         print 'done mythread'  myThread = MyThread () myThread.start()  a=5 while not myThread.wantQuit:     print 'hey'     if (a == 0):         break;     a = a-1;     time.sleep(1) myThread.wantQuit=1 print 'main thread done' 

what happens is instead of two threads i have the main one printing hey 4/6 times, then a dialog poping up asking for my input and the app being locked up until i enter it. WTF?!

want input hey hey hey hey abort with KeyboardInterrupt done mythread main thread done 

i am using PyScripter (it has debugging), i also tried pydle which doesnt seem to allow me to enter input and locks up eventually after running it once.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:39:14+00:00Added an answer on May 11, 2026 at 3:39 am

    The problem here is that raw_input waits for an enter to flush the input stream; check out its documentation. PyScripter is probably seeing that the program is waiting for an input and giving you an input box (don’t know for sure, never used it.)

    The program works exactly as I expect it to from the command line; the secondary thread blocks on the raw_input until I hit ‘q[enter]’, at which point the program ends.

    It is not, AFAICS, easy to check and see if a character is available in the input stream before blocking on a read call. You probably should check out this thread on how to read a character in a blocking fashion without requiring an [enter], and then this post on the challenge of reading a character without blocking at all.

    You can possibly use a combination of msvcrt.kbhit on windows and this recipe from the python FAQ to get the q character without requiring a keypress, but I’ll leave it as an exercise to the reader.

    Addendum: One thing you could do would be to use the select library to set a timeout on reads from the keyboard, which will make your program act more like you expect:

    import sys import threading import time import select  def timeout_read(n_chars):   r, _, _ = select.select([sys.stdin], [], [], 1)   return r[0].read(n_chars) if r else ''  class MyThread (threading.Thread):     def run (self):         try:             self.wantQuit = 0             while not self.wantQuit:                 print 'want input'                 button = timeout_read(1)                 if button == 'q':                     self.wantQuit=1         except KeyboardInterrupt:             self.wantQuit = 1             print 'abort with KeyboardInterrupt'         print 'done mythread'  myThread = MyThread () myThread.start()  a=5 while not myThread.wantQuit:     print 'hey'     if (a == 0):         break;     a = a-1;     time.sleep(1) myThread.wantQuit=1 print 'main thread done' 

    Note that you will still need to press ‘q[enter]’ with this solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a school project and i want to dedicate this for New Year,
Maybe I'm, just tired but I have a right click menu that I want
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't
my question is maybe a dumb one, but i cant help myself - i
Maybe is a dumb question, but I can't find the proper function: I have
There maybe some simlar questions to this but I can't see anything that really
Maybe it's because of the dark outside, but I can't get this Position geom_text
Maybe this is a poor question, but, I can't find a tutorial or even
This question is a continuation of this thread : In short: To solve my
I have a table like this below. And there is a div container with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.