Created
June 3, 2017 10:18
-
-
Save tatsuya6502/72599e2d9785ec7c7d5e50959d2a2333 to your computer and use it in GitHub Desktop.
check and set nonblocking option to stdin and stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fcntl, sys, os | |
def get_status(fileno): | |
fl = fcntl.fcntl(fileno, fcntl.F_GETFL) | |
if (fl & os.O_NONBLOCK) == os.O_NONBLOCK: | |
return "nonblocking" | |
else: | |
return "blocking" | |
def set_nonblocking(fileno): | |
fl = fcntl.fcntl(fileno, fcntl.F_GETFL) | |
return fcntl.fcntl(fileno, fcntl.F_SETFL, fl | os.O_NONBLOCK) | |
def set_blocking(fileno): | |
if get_status(fileno) == "nonblocking": | |
fl = fcntl.fcntl(fileno, fcntl.F_GETFL) | |
return fcntl.fcntl(fileno, fcntl.F_SETFL, fl - os.O_NONBLOCK) | |
else: | |
return 0 | |
stdin = sys.stdin.fileno() | |
stdout = sys.stdout.fileno() | |
print("stdin: %s, stdout: %s" % (get_status(stdin), get_status(stdout))) | |
print("") | |
print("making blocking") | |
set_blocking(stdin) | |
set_blocking(stdout) | |
print("stdin: %s, stdout: %s" % (get_status(stdin), get_status(stdout))) | |
print("") | |
print("making nonblocking") | |
set_nonblocking(stdin) | |
set_nonblocking(stdout) | |
print("stdin: %s, stdout: %s" % (get_status(stdin), get_status(stdout))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% uname -a | |
Linux mini-arch 4.11.3-1-ARCH #1 SMP PREEMPT Sun May 28 10:40:17 CEST 2017 x86_64 GNU/Linux | |
% python3 --version | |
Python 3.6.1 | |
% python3 nonblocking.py | |
stdin: blocking, stdout: blocking | |
making blocking | |
stdin: blocking, stdout: blocking | |
making nonblocking | |
stdin: nonblocking, stdout: nonblocking | |
% python2 --version | |
Python 2.7.13 | |
% python2 nonblocking.py | |
stdin: blocking, stdout: blocking | |
making blocking | |
stdin: blocking, stdout: blocking | |
making nonblocking | |
stdin: nonblocking, stdout: nonblocking |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment