日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python stdout stderr 一起输出_Python在保留顺序的同时分别从子进程stdout和stderr读取...

發(fā)布時間:2025/3/21 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python stdout stderr 一起输出_Python在保留顺序的同时分别从子进程stdout和stderr读取... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

小智..

6

寫入后,進程將數(shù)據(jù)寫入不同管道的順序?qū)G失。

您無法判斷在stderr之前是否已寫入stdout。

您可以嘗試在數(shù)據(jù)可用時以非阻塞方式同時從多個文件描述符中同時讀取數(shù)據(jù),但這只會最大程度地降低順序不正確的可能性。

該程序應(yīng)證明這一點:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os

import select

import subprocess

testapps={

'slow': '''

import os

import time

os.write(1, 'aaa')

time.sleep(0.01)

os.write(2, 'bbb')

time.sleep(0.01)

os.write(1, 'ccc')

''',

'fast': '''

import os

os.write(1, 'aaa')

os.write(2, 'bbb')

os.write(1, 'ccc')

''',

'fast2': '''

import os

os.write(1, 'aaa')

os.write(2, 'bbbbbbbbbbbbbbb')

os.write(1, 'ccc')

'''

}

def readfds(fds, maxread):

while True:

fdsin, _, _ = select.select(fds,[],[])

for fd in fdsin:

s = os.read(fd, maxread)

if len(s) == 0:

fds.remove(fd)

continue

yield fd, s

if fds == []:

break

def readfromapp(app, rounds=10, maxread=1024):

f=open('testapp.py', 'w')

f.write(testapps[app])

f.close()

results={}

for i in range(0, rounds):

p = subprocess.Popen(['python', 'testapp.py'], stdout=subprocess.PIPE

, stderr=subprocess.PIPE)

data=''

for (fd, s) in readfds([p.stdout.fileno(), p.stderr.fileno()], maxread):

data = data + s

results[data] = results[data] + 1 if data in results else 1

print 'running %i rounds %s with maxread=%i' % (rounds, app, maxread)

results = sorted(results.items(), key=lambda (k,v): k, reverse=False)

for data, count in results:

print '%03i x %s' % (count, data)

print

print "=> if output is produced slowly this should work as whished"

print " and should return: aaabbbccc"

readfromapp('slow', rounds=100, maxread=1024)

print

print "=> now mostly aaacccbbb is returnd, not as it should be"

readfromapp('fast', rounds=100, maxread=1024)

print

print "=> you could try to read data one by one, and return"

print " e.g. a whole line only when LF is read"

print " (b's should be finished before c's)"

readfromapp('fast', rounds=100, maxread=1)

print

print "=> but even this won't work ..."

readfromapp('fast2', rounds=100, maxread=1)

并輸出如下內(nèi)容:

=> if output is produced slowly this should work as whished

and should return: aaabbbccc

running 100 rounds slow with maxread=1024

100 x aaabbbccc

=> now mostly aaacccbbb is returnd, not as it should be

running 100 rounds fast with maxread=1024

006 x aaabbbccc

094 x aaacccbbb

=> you could try to read data one by one, and return

e.g. a whole line only when LF is read

(b's should be finished before c's)

running 100 rounds fast with maxread=1

003 x aaabbbccc

003 x aababcbcc

094 x abababccc

=> but even this won't work ...

running 100 rounds fast2 with maxread=1

003 x aaabbbbbbbbbbbbbbbccc

001 x aaacbcbcbbbbbbbbbbbbb

008 x aababcbcbcbbbbbbbbbbb

088 x abababcbcbcbbbbbbbbbb

總結(jié)

以上是生活随笔為你收集整理的python stdout stderr 一起输出_Python在保留顺序的同时分别从子进程stdout和stderr读取...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。