简体   繁体   English

将函数和参数传递给线程

[英]passing function and arguments to thread

i am calling a function ReadImages() in a t3 thread. 我正在t3线程中调用函数ReadImages() here is my code . 这是我的代码。

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

here is my thread implementation: 这是我的线程实现:

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

this code is running fine and i am getting the data from queues as required but its causing an exception and i do not know what is the reason for that. 这段代码运行良好,我正在根据需要从队列中获取数据,但它导致异常,我不知道这是什么原因。 i want to remove this exception. 我想删除此例外。 How would i do that ? 我该怎么办? here is the exception Training in thread... train 这是例外。

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

I'm not exactly sure where your 754th line of code is but double check if you have added commas between each value of your tuple. 我不确定您的第754行代码在哪里,但是请仔细检查是否在元组的每个值之间添加了逗号。 eg ((x,y) , (a,b) , (p,q)). 例如(((x,y),(a,b),(p,q))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM