在函数内部启动线程

时间:2018-11-30 12:46:06

标签: python python-2.7 global-variables python-multithreading

我正在尝试使用如下结构执行代码:

import things
...
class MyThreadRead(Thread):
    ...
    def run(self):
        global cap
        global frame_resized
        global netMain
        ...
        ret, frame = cap.read()
        frame_resized = cv2.resize(...)

...
...
def YOLO():
    ...
    global frame_resized
    global cap
    ...
    cap = cv2.VideoCapture(...)
    ...
    while True:
        ...
        readFrameThread.start()
        detections = detect(a, b, frame_resized, c)
        ...
    readFrameThread.join()
...
...
if __name__== "__main__":
    readFrameThread = MyThreadRead(1)
    YOLO()

执行此脚本时,在YOLO函数内部的函数检测行中出现此错误:

NameError: global name ´frame_resized´ is not defined

我应该在哪里声明全局变量?在YOLO函数内部还是外部?

1 个答案:

答案 0 :(得分:0)

您应该像这样在全局级别定义它

if __name__== "__main__":
    frame_resized = None
    readFrameThread = MyThreadRead(1)
    YOLO()

但是更好的是完全避免使用全局变量。有很多方法可以做到这一点,一种是创建数据容器并将其传递给所有各方。它看起来会像这样:

class Container:
    def __init__(self):
        self.cap = None
        self.frame_resized = None
        self.netMain  = None

class MyThreadRead(Thread):
    def __init__(self, container):
        self.container = container

    def run(self):
        ret, frame = self.container.cap.read()
        self.container.frame_resized = cv2.resize(...)

def YOLO(container, trd):
    container.cap = cv2.VideoCapture(...)
    ...
    while True:
        ...
        readFrameThread.start()
        detections = detect(a, b, container.frame_resized, c)
        ...
    trd.join()

if __name__== "__main__":
    container = Container()
    readFrameThread = MyThreadRead(1, container)
    YOLO(container, readFrameThread)

选择实现容器的方式时,请不要忘记,namedtuples是只读的,不适合您的情况。