2008年12月17日水曜日

標準入力を select で

Python でキーボード入力を select を介して処理するには,というメモ書き.
以下のように sys.stdin を select の read 用ファイルディスクリプタリストに
入れておけばいいらしい.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import sys
import select

def main():
""" docstring for main """
__rfds = []
__rfds.append(sys.stdin)

while True:
rfds = __rfds[:]
try:
rfd = select.select(rfds, [], [])
if rfd:
str = __rfds[0].readline()[:-1]
print str

except select.error, msg:
print "select failed: %s" % msg

#-----------------------------------------
if __name__ == "__main__":
main()

0 件のコメント: