python-3.x - How do get iter_lines to be nonblocking?
问题描述
Hi I am new to Python I have some code that looks like this.
import requests
import asyncio
async def get_http_stream():
res = requests.get('https://www.blah.com', stream=True)
for line in res.iter_lines(decode_unicode=True):
print(line)
async def wait_then_print(secs):
await asyncio.sleep(secs)
print(f' xxxxxx {secs} xxxxxx')
async def main():
await asyncio.wait([
wait_then_print(20),
get_http_stream(),
wait_then_print(10),
wait_then_print(0)
])
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
actual output:
every line from stream...
END
xxxxxx 0 xxxxxx
xxxxxx 10 xxxxxx
xxxxxx 20 xxxxxx
desired output:
xxxxxx 0 xxxxxx
some lines from stream...
xxxxxx 10 xxxxxx
more lines from stream...
END
xxxxxx 20 xxxxxx
What's the correct way to get my desired output?
解决方案
推荐阅读
- wordpress - 如何添加一个始终悬停在 woocommerce 网格中每个产品上的小徽标?
- php - 如何验证laravel中的文件输入?
- swift - SDWebImage,在 indexPath.row 中获取 url 图片
- react-native - 移动对象时无法插入颜色
- c# - 在 .NET Windows 服务中创建 API 端点
- python - Python ssl 套接字类型错误
- java - 通过jsp错误生成从服务器获取的docx文件
- node.js - 如何在 Vue.js 中使用 dotenv
- batch-file - CeRun.exe 未在 WinCE 上启动批处理文件
- ios - 如何检查 Swift 的子类中使用了哪个 ViewController