threading

main
luanhailiang 2023-09-22 13:59:08 +08:00
parent acfa96c3eb
commit 22ebe5d2d6
2 changed files with 26 additions and 8 deletions

32
app.py
View File

@ -2,6 +2,7 @@ import cv2
import json import json
import time import time
import requests import requests
import threading
import numpy as np import numpy as np
import supervision as sv import supervision as sv
@ -69,7 +70,10 @@ def worker(id,source,region=None,stream=False):
if not cap.isOpened(): if not cap.isOpened():
print("Error opening video stream.") print("Error opening video stream.")
while True: while True:
use = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
bgn = (time.time()*1000)
ret, frame = cap.read() ret, frame = cap.read()
use += " read:"+str((time.time()*1000) - bgn)
if not ret: if not ret:
print("Error read video stream.") print("Error read video stream.")
cap = cv2.VideoCapture(source) cap = cv2.VideoCapture(source)
@ -85,6 +89,7 @@ def worker(id,source,region=None,stream=False):
break break
continue continue
result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4)
use += " track:"+str((time.time()*1000) - bgn)
del(ret) del(ret)
del(frame) del(frame)
result = result[0] result = result[0]
@ -114,6 +119,7 @@ def worker(id,source,region=None,stream=False):
balls[name] = ball balls[name] = ball
names[name] = True names[name] = True
names = {} names = {}
use += " names:"+str((time.time()*1000) - bgn)
if count < region: if count < region:
continue continue
data = {"table":id,"balls":balls,"time":int(time.time()*1000)} data = {"table":id,"balls":balls,"time":int(time.time()*1000)}
@ -121,10 +127,13 @@ def worker(id,source,region=None,stream=False):
json_data = json.dumps(data) json_data = json.dumps(data)
balls = {} balls = {}
count = 0 count = 0
use += " dump:"+str((time.time()*1000) - bgn)
if not stream: if not stream:
yield json_data yield json_data
else: else:
yield f"data: {json_data}\n\n" yield f"data: {json_data}\n\n"
use += " yield:"+str((time.time()*1000) - bgn)
print(use)
except GeneratorExit: except GeneratorExit:
print("Client disconnected at", time.ctime()) print("Client disconnected at", time.ctime())
@ -134,21 +143,30 @@ def workerloop(stop_event,id,source,target=None,region=None):
try: try:
gen = worker(id,source,region) gen = worker(id,source,region)
for data in gen: for data in gen:
use = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
bgn = (time.time()*1000)
if stop_event.is_set(): if stop_event.is_set():
break break
json_data = json.loads(data) json_data = json.loads(data)
# 设置请求头 use += " loads:"+str((time.time()*1000) - bgn)
headers = {
"Content-Type": "application/json"
}
text = json.dumps({"content":json_data}) text = json.dumps({"content":json_data})
# print(text) use += " dumps:"+str((time.time()*1000) - bgn)
response = requests.post(target, data=text, headers=headers)
# print(response.text) # 创建并启动线程
thread = threading.Thread(target=post_request, args=(target, text))
thread.start()
use += " post:"+str((time.time()*1000) - bgn)
print("gen",use)
finally: finally:
gen.close() gen.close()
def post_request(url, data):
headers = {
"Content-Type": "application/json"
}
response = requests.post(url, data=data,headers=headers)
print(response.text)
if __name__ == '__main__': if __name__ == '__main__':
app.run("0.0.0.0",threaded=True) app.run("0.0.0.0",threaded=True)

View File

@ -27,4 +27,4 @@ def show():
if __name__ == '__main__': if __name__ == '__main__':
app.logger.setLevel(logging.ERROR) app.logger.setLevel(logging.ERROR)
app.run("0.0.0.0",threaded=True) app.run("0.0.0.0",port=8000,threaded=True)