多线程时循环未使用新变量运行

2024-10-01 02:35:13 发布

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

我目前正在做我大学最后一年的项目,我被困在我认为是线程的问题。我希望能够多次运行我的方法,但每次运行它都应该用新值更新一个变量。我正在查询一个API以获取userID,然后通过将其设置为全局变量将其传递到我的main方法中。你知道吗

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)

上面我将userID设置为global

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和状态,无论是开还是关。目前情况还不错。但是问题是,如果这个方法正在运行,并且想用一个新的userID再次运行它,那么它会一直工作到'while'摄影机.isopend():“当我走到这一步时,我没有发现任何错误

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()

在上面,我希望能够同时运行此方法的多个实例,并为每个实例使用不同的userId。你知道吗

if __name__ == '__main__':
    #mainNullPostMethod()
    #runDecision()
    while True:

        time.sleep(5)
        decision()

任何帮助和建议都将不胜感激。我不是python的高手,如果这是个愚蠢的问题,我很抱歉


Tags: to方法ifobjectpostcv2framenull
1条回答
网友
1楼 · 发布于 2024-10-01 02:35:13

首先不要使用全局变量,因为当从多个函数(在您的例子中是多个线程)as described in this answer更改时,很难跟踪全局变量。你知道吗

我看到的问题是在main函数中初始化userID的问题,这个函数用于生成线程,问题是即使在main中初始化userID = statusCopy,即使在decision中使用statusCopy = copy.deepcopy(tokenId)进行深度复制,它仍然会被任何并发的决策调用全局覆盖。你知道吗

让我们设想一下,第一次调用decision,初始化userID,然后为main生成一个线程,该线程利用了userID。现在我不确定执行main需要多长时间,但假设您使用sleep等待5秒钟,然后再次执行整个操作(第一个线程仍在运行)。现在您基本上更改了userID,第二次执行整个函数链,第一个线程开始使用修改后的userID,根据定义,这已经是一种不好的做法,因为您希望在第一个线程中使用特定的userID信息。我建议您将deepcopy传递给线程,并在main中初始化本地userID,这样它就不会被并发线程更改。你知道吗

另外,我不确定您是否希望执行while True并每5秒生成一次线程,也许您也应该在那里设置一个限制。你知道吗

相关问题 更多 >