python - 使用请求时如何从子进程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。
解决方案
推荐阅读
- python - 我如何通过 selenium / python 找到这个元素
- templates - 使用 Archetype 设计器并且无法选择 Root Archetype Id
- c# - Winium 无法检测 Office Addin 中的元素
- c# - 带有修订的嵌套项生成慢 SQL 的 Linq 查询
- python - 尝试通过它的Api将Excel从本地上传到A web
- python - 具有相同结构的多个模块的 Python Sphinx
- linux - 即使 container:path 正确,Docker cp 也会失败
- javascript - 在 express.js 中关闭浏览器时 Cookie 过期
- python - 生成行列和对角线之和相等的矩阵
- python - 有人可以帮我按我想要的方式订购这份清单吗?