回答編集履歴
1
関連リンクを追加
answer
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
調べてみるとユーザーがめいめいに実装しており、シグナルを使ったりスレッドを使ったりと様々でした。
|
4
4
|
|
5
|
-
そのなかで汎用性が高く使うのが簡単そうだったのが、[pnpnpn/timeout-decorator: Timeout decorator for Python](https://github.com/pnpnpn/timeout-decorator) というライブラリを使う方法で、input() 以外にも使えて便利そうでした。
|
5
|
+
そのなかで汎用性が高く使うのが簡単そうだったのが、[pnpnpn/timeout-decorator: Timeout decorator for Python](https://github.com/pnpnpn/timeout-decorator) というライブラリを使う方法で、input() 以外にも使えて便利そうでした。ただしシグナルを使っているため Windows では動かない可能性があります。
|
6
6
|
|
7
7
|
pnpnpn/timeout を使ったサンプルを示します:
|
8
8
|
|
@@ -22,4 +22,10 @@
|
|
22
22
|
print('{!r} が入力されました'.format(input_str))
|
23
23
|
except TimeoutError:
|
24
24
|
print('時間切れ!')
|
25
|
-
```
|
25
|
+
```
|
26
|
+
|
27
|
+
そのほかの事例:
|
28
|
+
|
29
|
+
* シグナルを使った実装: [Keyboard input with timeout in Python - Stack Overflow](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python)
|
30
|
+
* シグナルを使った実装: [Simple key input with time out on python. タイムアウト付きキー入力。](https://gist.github.com/alfredplpl/ef9fb85e07e5dcb7363d)
|
31
|
+
* スレッドやタイマーを使った実装: [Python 3 Timed Input - Stack Overflow](https://stackoverflow.com/questions/15528939/python-3-timed-input) ←Windows向けのコードも紹介されています(かなり苦しい方法ですが)
|