找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2530|回复: 0

[已解决] 调用subprocess.popen命令运行代码不会时时输出,但pycharm运行...

0

主题

0

帖子

0

积分

贫民

积分
0
jason_young_y 发表于 2019-11-22 11:22:11 | 显示全部楼层 |阅读模式
本帖最后由 jason_young_y 于 2019-11-22 13:52 编辑

#Python calls subprocess.popen
#Ro is the way of redis  Ro是redis的方法
#code is run in flask 代码在flask中执行

def Thread_CMD_By_Redis_Queue(uid,_cmd,  plat_name=None):
    RO.del_key(uid)
    def _cmd_file():
        _restul = subprocess.Popen(_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        print ("=============",_restul.pid)
        while subprocess.Popen.poll(_restul) is None:
            for line in iter(_restul.stdout.readline, b''):
                RO.queue_put(uid, line.decode('utf-8').strip())  #push in redis queue
        if subprocess.Popen.poll(_restul) != 0:
            for line in iter(_restul.stderr.readline, b''):
                RO.queue_put(uid, line.decode('utf-8').strip())  #push in redis queue
            RO.queue_put(uid, "plat call test: Fail") if \
                plat_name is None else RO.queue_put(uid,"{} call test: Fail".format(plat_name))
        else:
            RO.queue_put(uid, "plat call test: OK") if \
                plat_name is None else RO.queue_put(uid,"{} call test:OK".format(plat_name))
        RO.queue_put(uid,"")
    sub = SubThread(_cmd_file)
    sub.setDaemon(True)
    sub.start()

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表