multithreading - Python 2.7: Asynchronous function call for pre-fetching iterator -
i'm trying write pre-fetching iterator mxnet in python 2.7. basically, start 1 process loads convolutional neural network on gpu0 , waits other processes give data process. given data put through cnn, acts feature extractor , resulting features should stored in kind of future object blocking get. know can use multiprocessing module start process , queue input , have this:
import multiprocessing mp import numpy np def _async_listener(model_information, queue): # load model model = load_cnn(model_information) while true: queue_item = queue.get() if queue_item == none: return else: data = queue_item[0] future = queue_item[1] #process data preprocessed = do_some_preprocessing(data) features = model.forward(preprocessed ) #write features destination given future #and set kind of data_ready flag future.save(features) class async_extractor(): def __init__(self, model_information): self.q = mp.queue() self.p = mp.process(target=_async_listener, args=(model_information, self.q)) self.p.start() def add_job(self,data): target_destination = np.zeros(xdim,ydim,zdim) future = future(target_destination) self.q.put((data,future)) return future def shutdown(self): self.q.put(none) self.p.join() the main thread should like
feature_extractor = async_extractor(model_information) ... data = load_data() features_future = feature_extractor.add_job(data) ... other gpu features = features_future.get() #block until features ready .... useful features feature_extractor.shutdown() i know put queue pickled, not sure if there way create such future object in python 2.7. arrays can quite large, prefer solution works without copying around in memory. there in python?
Comments
Post a Comment