将函数和参数传递给线程

时间:2016-12-10 23:08:09

标签: python multithreading queue python-multithreading

我在ReadImages()帖子中调用函数t3。这是我的代码。

#ReadImages
def ReadImages(path,queue1,queue2,queue3):
    print('Training in thread...')

    # Create a list of images and a list of corresponding names
    (images, lables, names, id) = ([], [], {}, 0)
    namess=[]

    for (subdirs, dirs, files) in os.walk(fn_dir):
        for subdir in dirs:
            names[id] = subdir 
        namess.append(names[id])
        print "Names[id]", names[id]
            subjectpath = os.path.join(fn_dir, subdir)
            for filename in os.listdir(subjectpath):
                path = subjectpath + '/' + filename
                lable = id
                images.append(cv2.imread(path, 0))
                lables.append(int(lable))
            id += 1

    (im_width, im_height) = (112, 92)

    # Create a Numpy array from the two lists above
    (images, lables) = [numpy.array(lis) for lis in [images, lables]]
    print namess
    for i in range(len(images)):
        queue1.put(images[i])
    for l in range(len(lables)):
        queue2.put(lables[l])
    for n in range(len(names)):
        queue3.put(names[n])

    return (images,lables)

这是我的线程实现:

if __name__ == '__main__':


    images=[] 
    lables=[]
    names=[]
    queue1 = Queue.Queue()
    queue2 = Queue.Queue()
    queue3 = Queue.Queue()

    t3 = threading.Thread(target = ReadImages(path,queue1,queue2,queue3))
    print"train", t3
    t3.start()

    while not queue1.empty():
        images.append(queue1.get())
    while not queue2.empty():
        lables.append(queue2.get())
    while not queue3.empty():
        names.append(queue3.get())

此代码运行正常,我从队列中获取数据,但它导致异常,我不知道是什么原因。我想删除此异常。我该怎么做?这是例外 线程培训...... 火车

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: 'tuple' object is not callable

1 个答案:

答案 0 :(得分:0)

我不确定你的第754行代码在哪里,但仔细检查你是否在元组的每个值之间添加了逗号。例如((x,y),(a,b),(p,q))。