python程序在客户端连接后冻结

2024-04-19 20:27:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python程序,它使用flask进行视频流(独立JPEG图片序列)。我知道我应该先自己调试,但是一旦客户端连接索引.html只显示一个损坏的图像图标,计算机冻结。 我试过-

  • 在运行良好的linux上运行它。你知道吗
  • 禁用或启用线程,但没有效果。

编辑2:

它在Linux上工作是因为它安装了python2.7。在Python3中,它不显示任何错误,但不工作。在四个系统(包括windows和linux)上都试过了,这似乎是一个兼容性问题,因为它在所有使用python3的计算机上都失败了,但在python2上却没有。如何使它在python3中工作?你知道吗

编辑1:通过在gen()中的循环中添加time.sleep(1)来修复冻结,但是在gen()中仍然没有输出浏览器。已录制chrome开发者工具中的网络活动。这是一张截图- enter image description here

代码如下:

你知道吗应用程序类型(服务器)

#!/usr/bin/env python
from flask import Flask, render_template, Response

# emulated camera
from camera import Camera

app = Flask(__name__)


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


def gen(camera):
    """Video streaming generator function."""
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False, threaded=False)

你知道吗摄像头.py你知道吗

from time import time
class Camera(object):
    """An emulated camera implementation that streams a repeated sequence of
    files 1.jpg, 2.jpg and 3.jpg at a rate of one frame per second."""

    def __init__(self):
        self.frames = [open(f + '.jpg', 'rb').read() for f in ['1', '2', '3']]

    def get_frame(self):
        return self.frames[int(time()) % 3]

你知道吗索引.py(客户)

<html>
  <head>
    <title>Video Streaming Demonstration</title>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
    <img src="{{ url_for('video_feed') }}">
  </body>
</html>

正如我已经提到的,我不能使用调试器,因为整个计算机都冻结了立即.python版本-3.5和操作系统-windows 10。
如何修复它?你知道吗


Tags: fromimportselfapptimedefhtmlvideo
1条回答
网友
1楼 · 发布于 2024-04-19 20:27:16

看起来您可能需要再次检查从中到图像文件的路由应用程序类型,即确保它们位于同一目录中,或者如果它们位于/images目录中,请仔细检查文件的路径,或者检查它们是否在那里。你知道吗

作为测试,(git)克隆Miguel的项目并运行它,看看是否得到相同的结果。你知道吗

https://blog.miguelgrinberg.com/post/video-streaming-with-flask

相关问题 更多 >