多线程时循环不运行个新变量
Loop not running with new variables when multi threading
我目前正在做我最后一年的大学项目,我被困在我认为是线程问题的问题上。我希望能够多次 运行 我的方法,但每次它得到 运行 它应该用新值更新一个变量。我正在查询 API 以获取 userID,然后通过将其设置为全局变量将其传递到我的主要方法中。
def setup():
try:
global gun_cascade, camera, frameRate, property_id, length, firstFrame, framecount,i,increment,start_Time,end_Time,statusCopy,userID
gun_cascade = cv2.CascadeClassifier('cascade.xml')
camera = cv2.VideoCapture('gun.mp4')
if camera.isOpened() == False:
print("Can't open video, isOpened is empty exiting now.")
exit(0)
frameRate = camera.get(5)
property_id = int(cv2.CAP_PROP_FRAME_COUNT)
length = int(cv2.VideoCapture.get(camera, property_id))
firstFrame = None
count = 0
gun_exist = False
increment = 0
start_Time = 0
end_Time = 0
i = 0
except Exception as e:
print(e)
exit(0)
以上我将用户ID设置为全局
def decision():
setup()
user_object = listener.userIdListner()
tokenId = user_object.token
status = user_object.status
if user_object.status == "ON":
#status=statusCopy[:]
#aux = copy.deepcopy(matriz)
global statusCopy
statusCopy = copy.deepcopy(tokenId)
print("About to run mainscrt"+statusCopy)
#print(type(userID))
print(type(user_object))
post_object = listener.mainNullPostMethod()
post_object.null
print(post_object.null)
#opening a a new thread
Thread(target = main).start()
#post_object = listener.mainNullPostMethod()
#post_object.null
#print(post_object.null)
else:
print ("Program failed to run")
我在这里查询我的 API 以获取用户 ID 和打开或关闭的状态。目前这个 运行 没问题。但问题是,如果此方法是 运行ning 并且想要使用新的用户 ID 再次 运行 它,它会一直工作到 'while camera.isOpened():' 当我到达这一点时,我没有收到任何错误或任何东西
def main():
#printing out the userid to see if it's been changed
userID = statusCopy
print("The current userID is "+userID)
while isOpened:
framecount =0
framecount += 1
frameId = camera.get(1) #current frame number
(grabbed, frame) = camera.read()
if not grabbed:
break
# resize the frame, convert it to grayscale, and blur it
frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#gray = cv2.GaussianBlur(gray, (21, 21), 0)
#gray = cv2.dilate(gray, None, iterations=2)
#stuff to try in the future
#scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE, outputRejectLevels = True
gun = gun_cascade.detectMultiScale(gray, 3,5)
for (x,y,w,h) in gun:
randID = uuid.uuid4().hex
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
rects = gun[0]
neighbours = gun[0]
weights = gun[0]
if (frameId % math.floor(frameRate) == 1):
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 165, 0), 1)
cv2.imwrite('bin/' + userID+'-'+randID + '.jpg', frame)
if userID == "NULL":
print("failed due to user null")
break
print("working on pushing images to s3"+userID)
s3.uploadDirectory("bin/", "open-gun-recordings",userID)
picURL = "s3bucket.com/users/screenshots/"+userID+'/'+userID+'-'+randID+'.jpg'
text.fire(picURL)
cv2.imshow("Security Feed", frame)
key = cv2.waitKey(1) & 0xFF
camera.release()
cv2.destroyAllWindows()
以上我希望能够同时拥有此方法的多个实例 运行ning 并且每个实例具有不同的 userId。
if __name__ == '__main__':
#mainNullPostMethod()
#runDecision()
while True:
time.sleep(5)
decision()
如有任何帮助和建议,我们将不胜感激。我不是最擅长 python 如果这是一个愚蠢的问题,我深表歉意
首先,不要使用全局变量,它们很糟糕,因为当从多个函数(在您的情况下是多个线程)进行更改时,这会使事情难以跟踪 as described in this answer。
我看到的问题是在用于生成线程的 main
函数中初始化 userID
,问题是即使您在 [=11] 中初始化 userID = statusCopy
=],即使您在 decision
中使用 statusCopy = copy.deepcopy(tokenId)
进行深度复制,它仍然会被任何并发决策调用全局覆盖。
让我们想象一下,您第一次调用 decision
,初始化 userID
,然后为 main
生成一个线程,该线程使用 userID
.现在我不确定 main
执行需要多长时间,但是为了论证的缘故,我们假设您使用 sleep
等待 5 秒,然后再次执行整个操作(而第一个线程仍然是 运行)。现在,您基本上是通过第二次执行整个函数链来更改 userID
,并且第一个线程开始使用修改后的 userID
,根据定义,这已经是不好的做法,因为您想使用特定的 userID
信息与您的第一个线程。我的建议是将深度复制传递给线程并在 main
中初始化本地 userID
,这样它就不会被并发线程更改。
此外,我不确定您是否想要执行 while True
并每 5 秒生成一次线程,或许您也应该设置一个限制。
我目前正在做我最后一年的大学项目,我被困在我认为是线程问题的问题上。我希望能够多次 运行 我的方法,但每次它得到 运行 它应该用新值更新一个变量。我正在查询 API 以获取 userID,然后通过将其设置为全局变量将其传递到我的主要方法中。
def setup():
try:
global gun_cascade, camera, frameRate, property_id, length, firstFrame, framecount,i,increment,start_Time,end_Time,statusCopy,userID
gun_cascade = cv2.CascadeClassifier('cascade.xml')
camera = cv2.VideoCapture('gun.mp4')
if camera.isOpened() == False:
print("Can't open video, isOpened is empty exiting now.")
exit(0)
frameRate = camera.get(5)
property_id = int(cv2.CAP_PROP_FRAME_COUNT)
length = int(cv2.VideoCapture.get(camera, property_id))
firstFrame = None
count = 0
gun_exist = False
increment = 0
start_Time = 0
end_Time = 0
i = 0
except Exception as e:
print(e)
exit(0)
以上我将用户ID设置为全局
def decision():
setup()
user_object = listener.userIdListner()
tokenId = user_object.token
status = user_object.status
if user_object.status == "ON":
#status=statusCopy[:]
#aux = copy.deepcopy(matriz)
global statusCopy
statusCopy = copy.deepcopy(tokenId)
print("About to run mainscrt"+statusCopy)
#print(type(userID))
print(type(user_object))
post_object = listener.mainNullPostMethod()
post_object.null
print(post_object.null)
#opening a a new thread
Thread(target = main).start()
#post_object = listener.mainNullPostMethod()
#post_object.null
#print(post_object.null)
else:
print ("Program failed to run")
我在这里查询我的 API 以获取用户 ID 和打开或关闭的状态。目前这个 运行 没问题。但问题是,如果此方法是 运行ning 并且想要使用新的用户 ID 再次 运行 它,它会一直工作到 'while camera.isOpened():' 当我到达这一点时,我没有收到任何错误或任何东西
def main():
#printing out the userid to see if it's been changed
userID = statusCopy
print("The current userID is "+userID)
while isOpened:
framecount =0
framecount += 1
frameId = camera.get(1) #current frame number
(grabbed, frame) = camera.read()
if not grabbed:
break
# resize the frame, convert it to grayscale, and blur it
frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#gray = cv2.GaussianBlur(gray, (21, 21), 0)
#gray = cv2.dilate(gray, None, iterations=2)
#stuff to try in the future
#scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE, outputRejectLevels = True
gun = gun_cascade.detectMultiScale(gray, 3,5)
for (x,y,w,h) in gun:
randID = uuid.uuid4().hex
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
rects = gun[0]
neighbours = gun[0]
weights = gun[0]
if (frameId % math.floor(frameRate) == 1):
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 165, 0), 1)
cv2.imwrite('bin/' + userID+'-'+randID + '.jpg', frame)
if userID == "NULL":
print("failed due to user null")
break
print("working on pushing images to s3"+userID)
s3.uploadDirectory("bin/", "open-gun-recordings",userID)
picURL = "s3bucket.com/users/screenshots/"+userID+'/'+userID+'-'+randID+'.jpg'
text.fire(picURL)
cv2.imshow("Security Feed", frame)
key = cv2.waitKey(1) & 0xFF
camera.release()
cv2.destroyAllWindows()
以上我希望能够同时拥有此方法的多个实例 运行ning 并且每个实例具有不同的 userId。
if __name__ == '__main__':
#mainNullPostMethod()
#runDecision()
while True:
time.sleep(5)
decision()
如有任何帮助和建议,我们将不胜感激。我不是最擅长 python 如果这是一个愚蠢的问题,我深表歉意
首先,不要使用全局变量,它们很糟糕,因为当从多个函数(在您的情况下是多个线程)进行更改时,这会使事情难以跟踪 as described in this answer。
我看到的问题是在用于生成线程的 main
函数中初始化 userID
,问题是即使您在 [=11] 中初始化 userID = statusCopy
=],即使您在 decision
中使用 statusCopy = copy.deepcopy(tokenId)
进行深度复制,它仍然会被任何并发决策调用全局覆盖。
让我们想象一下,您第一次调用 decision
,初始化 userID
,然后为 main
生成一个线程,该线程使用 userID
.现在我不确定 main
执行需要多长时间,但是为了论证的缘故,我们假设您使用 sleep
等待 5 秒,然后再次执行整个操作(而第一个线程仍然是 运行)。现在,您基本上是通过第二次执行整个函数链来更改 userID
,并且第一个线程开始使用修改后的 userID
,根据定义,这已经是不好的做法,因为您想使用特定的 userID
信息与您的第一个线程。我的建议是将深度复制传递给线程并在 main
中初始化本地 userID
,这样它就不会被并发线程更改。
此外,我不确定您是否想要执行 while True
并每 5 秒生成一次线程,或许您也应该设置一个限制。