def thread_func(start, end): try: local_maxima = mlsl.maxima(img, img_type, col_offset, start, col_stride, row_stride, col_total, row_total, cols, end-start, distance) lock.acquire() try: output.extend(local_maxima) finally: lock.release() finally: done.release()
def maxima(img, img_type, col_offset, row_offset, col_stride, row_stride, col_total, row_total, cols, rows, distance, threads=2): if threads < 1: raise Error('the argument threads must be positive') if threads < 2: return mlsl.maxima(img, img_type, col_offset, row_offset, col_stride, row_stride, col_total, row_total, cols, rows, distance) output = [] lock = threading.RLock() done = threading.Semaphore(0) if threads > rows: threads = rows; def thread_func(start, end): try: local_maxima = mlsl.maxima(img, img_type, col_offset, start, col_stride, row_stride, col_total, row_total, cols, end-start, distance) lock.acquire() try: output.extend(local_maxima) finally: lock.release() finally: done.release() for j in range(threads): start = row_offset + int(round(1.0*j*rows/threads)) end = row_offset + int(round(1.0*(j+1)*rows/threads)) thread.start_new_thread(thread_func, (start,end)) for j in range(threads): done.acquire() return output