Can anyone help provide an example of how to use Faiss with python multiprocessing? Currently I can only load faiss index in each individual process, and in each process the index is loaded into its own memory (leading to large memory consumption).
Any build-in way I can load the index once and query from multiple processes in parallel?
You can use the Python multiprocessing.dummy module, see eg.
from multiprocessing.dummy import Pool as ThreadPool
index = ...
def do_search(i):
return index.search(x[i:i+1])
pool = ThreadPool(10)
results = pool.map(do_search, range(10))
@mdouze Thanks for your answer! I am not very familiar with Python's multi-threading. I know python has a global interpreter lock. Will about code make use more than 1 CPU? Thanks again!
Faiss (like numpy) releases the GIL so this should not be a problem.
AFAIK once the index is loaded, the search utilize all of my CPUs.
no activity, closing.
Faiss (like numpy) releases the GIL so this should not be a problem.
why not use multiprocess.Pool to concurrent?
Does the process pool thread pool make any difference to the use of faiss?