使用线程化Python的HTTP请求

使用线程化Python的HTTP请求,python,multithreading,http,python-requests,Python,Multithreading,Http,Python Requests,我想使用flask将视频流到我的web浏览器。因此,我遵循中的说明 在我的程序中,在流式传输视频时,我必须向另一个服务发出HTTP请求。 当我发出请求时,我的视频流正在停止 我的程序就是基于这些链接, 请帮助了解如何在锁定和HTTP请求中使用线程 我的示例代码:(其余与源代码相同) def generate(): 全局输出帧,锁,已识别,cnt 尽管如此: #等待获得锁 带锁: #检查输出帧是否可用,否则跳过 #循环的迭代 cnt+=1 如果cnt%50==0: isRecognized=真 如

我想使用flask将视频流到我的web浏览器。因此,我遵循中的说明

在我的程序中,在流式传输视频时,我必须向另一个服务发出HTTP请求。 当我发出请求时,我的视频流正在停止

我的程序就是基于这些链接, 请帮助了解如何在锁定和HTTP请求中使用线程

我的示例代码:(其余与源代码相同)

def generate():
全局输出帧,锁,已识别,cnt
尽管如此:
#等待获得锁
带锁:
#检查输出帧是否可用,否则跳过
#循环的迭代
cnt+=1
如果cnt%50==0:
isRecognized=真
如果outputFrame为无:
持续
(flag,encodedImage)=cv2.imencode(“.jpg”,outputFrame)
#确保帧已成功编码
如果没有标记:
持续
收益率(b'--frame\r\n'b'内容类型:image/jpeg\r\n\r\n'+bytearray(encodedImage)+b'\r\n')
def生成车辆():
#获取对输出帧和锁变量的全局引用
全局IsRecognited,outputFrame
url='myurl.com'
尽管如此:
#等待获得锁
带锁:
如果IsRecognited==True:
isRecognized=False
#以JPEG格式对帧进行编码
cv2.imwrite(“temp.jpg”,outputFrame)
打开(“临时jpg”、“rb”)作为c:
b64string=base64.b64encode(c.read())
b64string=str(b64string)[2:-1]

r=requests.post(url,data=b64string)#你必须在像nginx这样的web服务器后面运行你的flask应用程序,或者使用uwsgi让它同时处理多个请求。

print('\n\n',r'\n\n')
除了print语句之外,你似乎没有使用r的值,对吗?您的程序可以推迟打印并请求另一个线程或执行器中的url吗?
def generate():
    global outputFrame, lock, isRecognized, cnt
    while True:
        # wait until the lock is acquired
        with lock:
            # check if the output frame is available, otherwise skip
            # the iteration of the loop
            cnt+=1
            if cnt%50 == 0:
                isRecognized = True
            if outputFrame is None:
                continue
            (flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
            # ensure the frame was successfully encoded
            if not flag:
                continue
        yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')

def generate_car():
    # grab global references to the output frame and lock variables
    global isRecognized, outputFrame
    url = 'myurl.com'
    while True:
        # wait until the lock is acquired
        with lock:
            if isRecognized ==True:
                isRecognized = False
                # encode the frame in JPEG format
                cv2.imwrite("temp.jpg", outputFrame)
                with open("temp.jpg", "rb") as c:
                    b64string = base64.b64encode(c.read())
                    b64string=str(b64string)[2:-1]

                r = requests.post(url, data=b64string) # <-- this line is the problem

                r = json.loads(r.text)
                print('\n\n', r ,'\n\n')
                (__, encodedImage) = cv2.imencode(".jpg", outputFrame)
                # yield the output frame in the byte format
                yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + 
                    bytearray(encodedImage) + b'\r\n')