[英]Setting stdout to non-blocking in python
事先警告 :出於好奇,我在這里亂砍。 我沒有具體的理由去做我正在做的事情!
下面是在MacOS 10.12.5
上的Python 2.7.13
上完成的
我正在用python進行攻擊,我覺得如果我做了stdout
nonblocking會發生什么事情會很有趣
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
對fcntl
的調用肯定是成功的。 然后我嘗試寫入大量數據(大於OSX上管道的最大緩沖區大小 - 這是65536字節)。 我以各種方式做到並獲得不同的結果,有時是例外,有時似乎是一個很難的失敗。
情況1
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
sys.stdout.write("A" * 65537)
except Exception as e:
time.sleep(1)
print "Caught: {}".format(e)
# Safety sleep to prevent quick exit
time.sleep(1)
這總是引發異常Caught: [Errno 35] Resource temporarily unavailable
。 我覺得這很有道理。 更高級別的文件對象包裝器告訴我寫入調用失敗。
案例2
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
sys.stdout.write("A" * 65537)
except Exception as e:
print "Caught: {}".format(e)
# Safety sleep to prevent quick exit
time.sleep(1)
這有時會引發異常Caught: [Errno 35] Resource temporarily unavailable
或有時沒有捕獲異常,我看到以下輸出:
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
案例3
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
sys.stdout.write("A" * 65537)
except Exception as e:
print "Caught: {}".format(e)
# Safety sleep to prevent quick exit
time.sleep(1)
print "Slept"
這有時會引發異常Caught: [Errno 35] Resource temporarily unavailable
或有時沒有捕獲異常,我只看到“睡眠”。 似乎通過print
“睡眠”我沒有收到案例2中的錯誤消息。
案例4
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
os.write(sys.stdout.fileno(), "A" * 65537)
except Exception as e:
print "Caught: {}".format(e)
# Safety sleep to prevent quick exit
time.sleep(1)
總是好的!
案例5
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
try:
print os.write(sys.stdout.fileno(), "A" * 65537)
except Exception as e:
print "Caught: {}".format(e)
# Safety sleep to prevent quick exit
time.sleep(1)
這有時是好的,或者有時打印close failed in file object destructor
錯誤消息中的close failed in file object destructor
。
我的問題是,為什么在python中這樣會失敗? 我在這里做了一些根本不好的事情 - 使用python還是在系統級別?
當寫入已經失敗時,似乎以某種方式寫入stdout太快會導致錯誤消息。 該錯誤似乎不是一個例外。 不知道它來自哪里。
NB我可以在C中編寫等效的程序,它可以正常工作:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/fcntl.h>
#include <unistd.h>
int main(int argc, const char * argv[])
{
const size_t NUM_CHARS = 65537;
char buf[NUM_CHARS];
// Set stdout non-blocking
fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);
// Try to write a large amount of data
memset(buf, 65, NUM_CHARS);
size_t written = fwrite(buf, 1, NUM_CHARS, stdout);
// Wait briefly to give stdout a chance to be read from
usleep(1000);
// This will be written correctly
sprintf(buf, "\nI wrote %zd bytes\n", written);
fwrite(buf, 1, strlen(buf), stdout);
return 0;
}
這是有趣的。 到目前為止,我發現了一些事情:
情況1
這是因為sys.stdout.write
將寫入所有字符串或拋出異常,這在使用O_NONBLOCK
時不是所需的行為。 當底層的write
調用返回EAGAIN
(OS X上的Errno 35)時,應該再次嘗試使用剩余的數據。 應該使用os.write
,並檢查返回值以確保寫入所有數據。
此代碼按預期工作:
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
def stdout_write(s):
written = 0
while written < len(s):
try:
written = written + os.write(sys.stdout.fileno(), s[written:])
except OSError as e:
pass
stdout_write("A" * 65537)
案例2
我懷疑此錯誤消息是由https://bugs.python.org/issue11380引起的:
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
我不確定為什么它有時會被調用。 這可能是因為在except
語句中有一個print
,它試圖使用相同的stdout
來寫入失敗。
案例3
這與案例1類似。此代碼始終適用於我:
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
def stdout_write(s):
written = 0
while written < len(s):
try:
written = written + os.write(sys.stdout.fileno(), s[written:])
except OSError as e:
pass
stdout_write("A" * 65537)
time.sleep(1)
print "Slept"
案例4
確保檢查os.write
的返回值,我懷疑沒有成功寫入完整的65537字節。
案例5
這與案例2類似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.