【问题标题】:real time in python subprocess not working on windows consolepython子进程中的实时无法在Windows控制台上运行
【发布时间】:2018-03-10 09:44:15
【问题描述】:

我正在尝试运行一个 python 脚本(激活器),它将运行另一个脚本(客户端)并在一定的时间上限后终止它。 (本例中为 5 秒)。

活化剂

import random, sys, os, socket
import time, datetime
import subprocess
from threading import Thread
from multiprocessing import Queue


class deamon_pckg:
    def __init__(self, msg_type, info, exeption=None):
        self.msg_type = msg_type
        self.info = info
        self.exception = exeption


class Process_comm:
    @staticmethod
    def deamon(process, q):
        while process.poll() is None:
            out = process.stdout.readline().rstrip()
            if out != '':
                d_pckg = deamon_pckg('cmd', out)
                q.put(d_pckg)

        exit_code = process.returncode
        if exit_code != 0:
            err_msg = process.stderr.read()
        else:
            err_msg = None
        d_pckg = deamon_pckg('EXIT', exit_code, err_msg)
        q.put(d_pckg)

    def __init__(self, process):
        self.process = process
        self.q = Queue()
        self.d = Thread(target=Process_comm.deamon, args=(self.process, self.q))
        self.d.start()

    def close(self):
        if self.process.returncode is None:
            self.terminate()
        self.d.join()

    def terminate(self):
        self.process.terminate()

    def get(self, blocking=False):
        if blocking:
            return self.q.get()
        else:
            if self.q.empty():
                return None
            else:
                return self.q.get()


if __name__ == '__main__':
    print "Activator online"
    start_time = datetime.datetime.now()
    process = subprocess.Popen(['python', "C:\work\Router\src\demiborg.py"],
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    d = Process_comm(process)


    while True:
        delta = (datetime.datetime.now() - start_time)
        dpckg = d.get()
        if delta.seconds >= 5:
            d.terminate()
            break
        if dpckg is None:
            time.sleep(0.1)
        else:
            if dpckg.msg_type == 'EXIT':
                print "[{}]\t{}".format(dpckg.msg_type, dpckg.info)
                if dpckg.exception is not None:
                    print "{}".format(dpckg.exception)
                break
            else:
                print "[{}]\t{}".format(dpckg.msg_type, dpckg.info)
    d.close()

客户:

import random, sys, os, socket
import time, datetime


def get_time_stamp():
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%f:%S:%M:%H:%d:%m:%Y')
    return st

def put(fout,msg):
    fout.write('[{:>30}]'.format(get_time_stamp()) + '\t' + msg + '\n')
    fout.flush
    print msg


if __name__ == '__main__':
    fout = open('demiborg.txt','wb')
    put(fout,"Start DemiBorg")
    for i in range(20):
        put(fout,"time - {}".format(i))
        time.sleep(0.5)

    put(fout,'End')
    fout = open('ERROR', 'wb')
    fout.close()

如果我跑了

Activator online
[cmd]	Start DemiBorg
[cmd]	time - 0
[cmd]	time - 1
[cmd]	time - 2
[cmd]	time - 3
[cmd]	time - 4
[cmd]	time - 5
[cmd]	time - 6
[cmd]	time - 7
[cmd]	time - 8
[cmd]	time - 9

Process finished with exit code 0

但是当我通过 Windows 控制台运行它时,我根本没有得到任何输入(就像客户端没有运行一样)。

C:\work\Router\src>C:\work\Router\src\plygrnd_activator.py
Activator online

C:\work\Router\src>

此外,如果 a 没有杀死客户端,我注意到只有在客户端完成运行后,所有消息才作为一个块一起出现。为什么只使用 pycharm 它工作正常但在控制台上我没有得到实时反馈? 目的是在控制台上运行。 谢谢!

【问题讨论】:

    标签: python-2.7 subprocess real-time windows-console


    【解决方案1】:

    解决方案,问题出在缓冲区大小上。

    而不是使用:

        process = subprocess.Popen(['python', "C:\work\Router\src\demiborg.py"],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)

    我在“python”之后添加了“-u”标志:

        process = subprocess.Popen(['python','-u', "C:\work\Router\src\demiborg.py"],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)

    【讨论】:

      猜你喜欢
      • 2019-08-26
      • 2020-10-27
      • 2015-10-18
      • 2015-03-03
      • 2015-11-18
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多