LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-09-2009, 03:04 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
python: reading from stdin with a timeout


Hi!

I know it's possible to read from the standard input:

Code:
line=sys.stdin.readline()
However, is it possible to set a timeout to the operation somehow?

Thanks in advance.
 
Old 07-09-2009, 03:41 PM   #2
CSandman
Member
 
Registered: Mar 2007
Distribution: Xubuntu
Posts: 33

Rep: Reputation: 16
You could fork a child that would do the read and then in the parent you could use sleep to wait for your timeout length and then just kill the child if it's still waiting. Off the top of my head I don't remember how the python code would look but it's pretty simple.
 
Old 07-09-2009, 03:43 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
I'm trying with threading.Timer
 
Old 07-09-2009, 03:47 PM   #4
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
Hell, it doesn't exit if sys.exit() is called from the function called from the Timer. Is that because of the call being in another thread? or because the readline() will refuse to quit?
 
Old 07-09-2009, 03:55 PM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
Got it with a signal. Found this:

http://www.velocityreviews.com/forum...nd-prompt.html
 
Old 07-09-2009, 03:59 PM   #6
CSandman
Member
 
Registered: Mar 2007
Distribution: Xubuntu
Posts: 33

Rep: Reputation: 16
Threading.Timer() is not what you want because it executes a command after some time. I was thinking you could fork a child and kill it after the timeout but that doesn't work because in order to get values back from the child you need to use a pipe() and os.read(), but os.read() is a blocking call...

let me ponder
 
Old 07-09-2009, 04:00 PM   #7
CSandman
Member
 
Registered: Mar 2007
Distribution: Xubuntu
Posts: 33

Rep: Reputation: 16
good find eantoranz I'll have to remember that trick!!
 
  


Reply

Tags
python, readline, stdin, timeout


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timeout function using Python vishnu_sreekumar Programming 1 08-03-2009 04:52 AM
Python: Stdin and python apps fighting? enigma_0Z Programming 2 07-15-2008 08:45 AM
[SOLVED] reading from stdin lord_didger Programming 5 01-16-2008 09:27 AM
Reading from stdin till eof stuck when sending multiple commands fredyk_ Linux - Software 1 04-25-2006 02:23 AM
java - reading from stdin greg108 Programming 4 01-13-2005 03:29 PM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration