[Tutor] Sys.stdin Question
John Fouhy
john at fouhy.net
Wed Jan 14 02:45:53 CET 2009
2009/1/14 Damon Timm <damontimm at gmail.com>:
> This works when I do have something coming via stdin ... but if I run
> the script without piping something first ... it just sits there (I
> assume, waiting for some stdin) ...
>
> How do I tell it: if there is no stdin, just move on?
This might work:
import select, sys
def isData():
return select.select([sys.stdin], [], [], 0) ==
([sys.stdin], [], [])
if isData():
print 'You typed:', sys.stdin.read()
else:
print 'Nothing to see here.'
I say "might" because it is OS-dependent, but I guess you are using unix/linux.
Source: http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/
I found that by searching for "python stdin non-blocking". This is
because "blocking" is jargon for "waiting until something happens".
In this case, stdin.read() is blocking until it sees some data with an
EOF.
HTH!
--
John.
More information about the Tutor
mailing list