小智..

6

写入后,进程将数据写入不同管道的顺序将丢失。

您无法判断在stderr之前是否已写入stdout。

您可以尝试在数据可用时以非阻塞方式同时从多个文件描述符中同时读取数据,但这只会最大程度地降低顺序不正确的可能性。

该程序应证明这一点:

#!/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)

并输出如下内容:

=> 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

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

  1. python stdout stderr 一起输出_Python捕获stdout/stderr并在看到outpu的同时记录到文件

    所以我有一个python脚本,它试图自动化整个构建过程.因此,它不仅调用python文件,还调用shell脚本.maven等,所以我需要的是一个方法/过程/库,将stdout和stderr的输出捕获到 ...

  2. python以垂直方式输出_python学习笔记

    Author: maddock Date: 2015-03-15 21:42:01 (暂时没有整理) python json文件处理 #coding:utf-8 importjson#data = [ ...

  3. python数据的格式输出_Python格式化输出

    "%"的使用 格式符 描述 %s 字符串 (采用str()的显示) %r 字符串 (采用repr()的显示) %c 单个字符及其ASCII码 %u 整数(无符号) %b 二进制整数 ...

  4. python一个文本循环输出_Python实现动态循环输出文字功能

    在一些公共场所经常可以看到一些动态提示的文字,以下代码即为文字的循环变化代码: import sys import time def print_act(word): print('新春佳节快乐'+' ...

  5. python如何多行输出_python换行输出 Python里具体怎么用\n换行输出一个数字?

    python里 如何把每打印10个数就换行的实现 print("每输出十个数字换行,共计输出100个:")for num in range(1,100):#循环一百次 print( ...

  6. python stdout stderr 一起输出_python – 使用subprocess.Popen()时,stderr和stdout没有输出

    我正在使用 Python来自动化SVN提交,我想将SVN命令的输出写入日志文件.我有的代码可以使SVN运行,但问题是在成功提交时,子进程调用不会返回我的日志的任何输出. 当我手动运行SVN时,通过比较 ...

  7. python stdout stderr 一起输出_Python日志记录在stdout和stderr之间拆分

    Is it possible to have python logging messages which are INFO or DEBUG to go to stdout and WARNING o ...

  8. python右对齐格式化输出_Python中格式化输出的两种方法介绍

    本篇文章给大家带来的内容是关于Python中格式化输出的两种方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 方式一:使用占位符 % 常用占位符:% s (s = string ...

  9. python数据的格式输出_python

    本文主要由Python String Format 一文翻译整理而来,在python中使用 % 进行格式化字符串由来已久,不过在python2.7+ 的版本中引入了新的格式化字符串的方法.虽然%号的方 ...

最新文章

  1. 转 知道这20个正则表达式,能让你少写1,000行代码
  2. C#实现简体繁体转换代码示例
  3. JS限制表单输入特效代码
  4. 父子沪c转大牌过户_机动车异地过户(转籍)
  5. linux复制以a开头的文件,linux部分试题
  6. LNMMP架构的实现
  7. 在线JSON转io-ts工具
  8. python是哪个专业学的-专业python培训学校
  9. AE “每用户订阅上的所有人SID 不存在”
  10. cmd批量修改文件名 增加文字_[Windows应用技巧][cmd篇][批量更改文件名]
  11. 备份路由器配置文件到服务器,手把手教您如何进行备份Cisco路由器配置
  12. 江苏省考计算机类包括哪些专业,2019年江苏省公务员考试计算机类包括哪些专业..._公务员考试_帮考网...
  13. IDEA maven项目使用Junit报错 java: 程序包org.junit不存在
  14. 简单20行python代码_就这么简单!20行Python代码爬取腾讯视频
  15. 眼底病php 是什么病,眼底病常见的7种类型 你都需要了解清楚!
  16. 视频教程-Kali Linux渗透测试全程课与脚本语言编程系列课程-渗透测试
  17. agc024F Simple Subsequence Problem
  18. 腾讯广告算法大赛(即腾讯社交广告算法大赛)
  19. JVM - 垃圾回收相关算法
  20. 量化投资发展史上的那些“决定性瞬间”

热门文章

  1. 产品经理必备知识之网页设计系列(三)-移动端适配无障碍设计及测试
  2. MATLAB时间序列的排序函数
  3. QT中写一个求QVector容器中数据均值的函数
  4. Hadoop报错信息:Job not successful. Error: # of failed Map Tasks exceeded allowed limit. FailedCount: 4.
  5. MyBatis的架构设计以及实例分析--转
  6. UML建模——用例图(Use Case Diagram)
  7. 【风控体系】互联网反欺诈体系漫谈
  8. 【项目实战】SQL :部门花名册PBI展示
  9. 模拟上帝之手的对抗博弈——GAN背后的数学原理
  10. 人人皆可大数据!SACC教你玩转阿里ODPS