首页 > 解决方案 > 使用请求时如何从子进程popen python脚本获取实时输出

问题描述

测试.py:

import os,subprocess,sys

cmd=['python', 'hellodude.py']

os.environ["PYTHONUNBUFFERED"] = "1"

process = subprocess.Popen(
    cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)

while True:
    out = process.stdout.read(1)
    if out == '' and process.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()

你好.py:

import time
import requests as req

print 'hey'
time.sleep(5)
print 'cheers'

我安装了 pip requests 和 requests[security] 如果您注释掉 requests import,它可以工作。如何解决?如果重要的话,在 linux 或 windows 上使用 python 2.7。

标签: pythonpython-requests

解决方案


推荐阅读