<bdo id='8Z8YF'></bdo><ul id='8Z8YF'></ul>
    <tfoot id='8Z8YF'></tfoot>

    <small id='8Z8YF'></small><noframes id='8Z8YF'>

    1. <legend id='8Z8YF'><style id='8Z8YF'><dir id='8Z8YF'><q id='8Z8YF'></q></dir></style></legend>
    2. <i id='8Z8YF'><tr id='8Z8YF'><dt id='8Z8YF'><q id='8Z8YF'><span id='8Z8YF'><b id='8Z8YF'><form id='8Z8YF'><ins id='8Z8YF'></ins><ul id='8Z8YF'></ul><sub id='8Z8YF'></sub></form><legend id='8Z8YF'></legend><bdo id='8Z8YF'><pre id='8Z8YF'><center id='8Z8YF'></center></pre></bdo></b><th id='8Z8YF'></th></span></q></dt></tr></i><div id='8Z8YF'><tfoot id='8Z8YF'></tfoot><dl id='8Z8YF'><fieldset id='8Z8YF'></fieldset></dl></div>

    3. python:非阻塞子进程,检查标准输出

      python: nonblocking subprocess, check stdout(python:非阻塞子进程,检查标准输出)

      <small id='zuril'></small><noframes id='zuril'>

            <tbody id='zuril'></tbody>
        • <i id='zuril'><tr id='zuril'><dt id='zuril'><q id='zuril'><span id='zuril'><b id='zuril'><form id='zuril'><ins id='zuril'></ins><ul id='zuril'></ul><sub id='zuril'></sub></form><legend id='zuril'></legend><bdo id='zuril'><pre id='zuril'><center id='zuril'></center></pre></bdo></b><th id='zuril'></th></span></q></dt></tr></i><div id='zuril'><tfoot id='zuril'></tfoot><dl id='zuril'><fieldset id='zuril'></fieldset></dl></div>
            <tfoot id='zuril'></tfoot>
                <bdo id='zuril'></bdo><ul id='zuril'></ul>
                <legend id='zuril'><style id='zuril'><dir id='zuril'><q id='zuril'></q></dir></style></legend>

              • 本文介绍了python:非阻塞子进程,检查标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                好的,我要解决的问题是:

                Ok so the problem I'm trying to solve is this:

                我需要运行一个设置了一些标志的程序,检查其进度并向服务器报告.所以我需要我的脚本来避免在程序执行时阻塞,但我还需要能够读取输出.不幸的是,我认为 Popen 提供的任何方法都不会在不阻塞的情况下读取输出.我尝试了以下,这有点hack-y(我们是否允许从两个不同的对象读取和写入同一个文件?)

                I need to run a program with some flags set, check on its progress and report back to a server. So I need my script to avoid blocking while the program executes, but I also need to be able to read the output. Unfortunately, I don't think any of the methods available from Popen will read the output without blocking. I tried the following, which is a bit hack-y (are we allowed to read and write to the same file from two different objects?)

                import time
                import subprocess
                from subprocess import *
                with open("stdout.txt", "wb") as outf:
                    with open("stderr.txt", "wb") as errf:
                        command = ['Path\To\Program.exe', 'para', 'met', 'ers']
                        p = subprocess.Popen(command, stdout=outf, stderr=errf)
                        isdone = False
                        while not isdone :
                            with open("stdout.txt", "rb") as readoutf: #this feels wrong
                                for line in readoutf:
                                    print(line)
                            print("waiting...\r\n")
                            if(p.poll() != None) :
                                done = True
                            time.sleep(1)
                        output = p.communicate()[0]    
                        print(output)
                

                不幸的是,Popen 似乎直到命令终止后才写入我的文件.

                Unfortunately, Popen doesn't seem to write to my file until after the command terminates.

                有人知道这样做的方法吗?我并不热衷于使用 python,但我确实需要在同一个脚本中向服务器发送 POST 请求,因此 python 似乎比 shell 脚本更容易.

                Does anyone know of a way to do this? I'm not dedicated to using python, but I do need to send POST requests to a server in the same script, so python seemed like an easier choice than, say, shell scripting.

                谢谢!会

                推荐答案

                基本上你有3个选择:

                1. 使用threading在另一个线程中读取而不阻塞主线程.
                2. select在标准输出上,标准错误而不是 communicate.这样您就可以在数据可用时进行读取,避免阻塞.
                3. 让一个库来解决这个问题,twisted是一个显而易见的选择.
                1. Use threading to read in another thread without blocking the main thread.
                2. select on stdout, stderr instead of communicate. This way you can read just when data is available and avoid blocking.
                3. Let a library solve this, twisted is a obvious choice.

                这篇关于python:非阻塞子进程,检查标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                1. <legend id='NYle3'><style id='NYle3'><dir id='NYle3'><q id='NYle3'></q></dir></style></legend>

                      <i id='NYle3'><tr id='NYle3'><dt id='NYle3'><q id='NYle3'><span id='NYle3'><b id='NYle3'><form id='NYle3'><ins id='NYle3'></ins><ul id='NYle3'></ul><sub id='NYle3'></sub></form><legend id='NYle3'></legend><bdo id='NYle3'><pre id='NYle3'><center id='NYle3'></center></pre></bdo></b><th id='NYle3'></th></span></q></dt></tr></i><div id='NYle3'><tfoot id='NYle3'></tfoot><dl id='NYle3'><fieldset id='NYle3'></fieldset></dl></div>
                        • <bdo id='NYle3'></bdo><ul id='NYle3'></ul>

                            <tbody id='NYle3'></tbody>

                          <small id='NYle3'></small><noframes id='NYle3'>

                          <tfoot id='NYle3'></tfoot>