Python 使用 2 个线程获得 'object is not callable'

Python get 'object is not callable' with 2 threads

当我运行下面的代码时,我得到了一个异常

# System
import time
import logging
import sys
import os
import threading
# cv2 and helper:
import cv2

class inic_thread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
        print "Starting " + self.name
        if self.counter == 1: capture_continuos()
        elif self.counter == 2: face_search()

def capture_continuos():
    #os.system('python capture_continuos.py')
    while(1):    
        print 'a'

def face_search():
    # atributes
    pool = []
    path_pool = './pool/'

    while(1):
        pool_get = os.listdir(path_pool)
        if len(pool_get) > 0:
            #print(str(len(pool_get))+' images in the pool')
            for image in pool_get:
                print(image)
                os.system('python face_search.py -i '+str(image))
        else:
            print('Empty Pool')

try:
    capture_continuos = inic_thread(1, "capture_continuos_1", 1)
    face_search_2 = inic_thread(2, "face_search_2", 2)

    capture_continuos.start()
    face_search_2.start()
except:
    print("Error: unable to start thread")  

但这对我来说没有意义,因为其中一个线程 运行 正常,(face_search) 但另一个线程出现此异常。

Starting capture_continuos_1
Exception in thread capture_continuos_1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "main.py", line 44, in run
    if self.counter == 1: capture_continuos()
TypeError: 'inic_thread' object is not callable

我做错了什么? 我 运行 在 Raspberry Pi 3 模型 B 中 Ubuntu MATE 14.04; Python 2.7.12

在脚本的底部,您重新定义了变量 capture_continuos 并为其分配了线程对象。

另外如前所述,终止线程最好调用 os._exit() 而不是 sys.exit()