栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

对子进程的非阻塞读取.Python中的PIPE

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

对子进程的非阻塞读取.Python中的PIPE

fcntl
select
asyncproc
不会在这种情况下帮助。

不管使用什么操作系统,一种可靠地读取流而不阻塞的可靠方法是使用

Queue.get_nowait()

import sysfrom subprocess import PIPE, Popenfrom threading  import Threadtry:    from queue import Queue, Emptyexcept importError:    from Queue import Queue, Empty  # python 2.xON_POSIX = 'posix' in sys.builtin_module_namesdef enqueue_output(out, queue):    for line in iter(out.readline, b''):        queue.put(line)    out.close()p = Popen(['myprogram.exe'], stdout=PIPE, bufsize=1, close_fds=ON_POSIX)q = Queue()t = Thread(target=enqueue_output, args=(p.stdout, q))t.daemon = True # thread dies with the programt.start()# ... do other things here# read line without blockingtry:  line = q.get_nowait() # or q.get(timeout=.1)except Empty:    print('no output yet')else: # got line    # ... do something with line


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/610529.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号