Wait for input, but only for a certain amount of time

Josiah Carlson jcarlson at nospam.uci.edu
Tue Feb 24 02:05:16 EST 2004


Pif Paf wrote:

> I have a process, and my process wants to wait for input but only for
> a certain amount of time. The input might be coming from a Python file
> object (created with popen) or a TCP socket. 
> 
> What I want is something like:
> 
>    s = getTimedInput(source, maxDelay)
> 
> where:
>    source = the source of the input
>    maxDelay = the maximum amount of time we're going to wait for input,
>       in milliseconds. We wait this amount of time, or a shorter time if we 
>       receive a "\n"(1) in the character stream
>    s = a string containing the input that was received
> 
> 
> (1): or whatever delimiter character we're using.
> 

If it is a socket on any platform, use select.select().
If it is a file in *nix, use select.select().
If it is a file in windows, check pywin32 (http://sf.net/projects/pywin32)

  - Josiah



More information about the Python-list mailing list