This commit is contained in:
Colombo 2020-01-05 13:58:25 +04:00
parent 21b25038ac
commit ea33541177
2 changed files with 92 additions and 100 deletions

View file

@ -1,5 +1,6 @@
import multiprocessing
import operator
import pickle
import traceback
from pathlib import Path
@ -62,22 +63,18 @@ class SampleHost:
if result is None:
result = SampleHost.load_face_samples( Path_utils.get_image_paths(samples_path) )
samples[sample_type] = mp_utils.ListHost()
if sample_type == SampleType.FACE_TEMPORAL_SORTED:
result = SampleHost.upgradeToFaceTemporalSortedSamples(result)
result_dumped = pickle.dumps(result)
del result
result = pickle.loads(result_dumped)
samples[sample_type] = mp_utils.ListHost(result)
list_host = samples[sample_type]
clis = [ list_host.create_cli() for _ in range(number_of_clis) ]
if result is not None:
while True:
if len(result) == 0:
break
items = result[0:10000]
del result[0:10000]
clis[0].extend(items)
return clis
return samples[sample_type]

View file

@ -1,25 +1,22 @@
import multiprocessing
import threading
import time
import traceback
import numpy as np
import numpy as np
class Index2DHost():
"""
Provides random shuffled 2D indexes for multiprocesses
"""
def __init__(self, indexes2D, max_number_of_clis=128):
def __init__(self, indexes2D):
self.sq = multiprocessing.Queue()
self.cqs = [ multiprocessing.Queue() for _ in range(max_number_of_clis) ]
self.n_clis = 0
self.max_number_of_clis = max_number_of_clis
self.cqs = []
self.clis = []
self.thread = threading.Thread(target=self.host_thread, args=(indexes2D,) )
self.thread.daemon = True
self.thread.start()
self.p = multiprocessing.Process(target=self.host_proc, args=(indexes2D, self.sq, self.cqs) )
self.p.daemon = True
self.p.start()
def host_proc(self, indexes2D, sq, cqs):
def host_thread(self, indexes2D):
indexes_counts_len = len(indexes2D)
idxs = [*range(indexes_counts_len)]
@ -30,6 +27,8 @@ class Index2DHost():
idxs_2D[i] = indexes2D[i]
shuffle_idxs_2D[i] = []
sq = self.sq
while True:
while not sq.empty():
obj = sq.get()
@ -44,7 +43,7 @@ class Index2DHost():
shuffle_idxs = idxs.copy()
np.random.shuffle(shuffle_idxs)
result.append(shuffle_idxs.pop())
cqs[cq_id].put (result)
self.cqs[cq_id].put (result)
elif cmd == 1: #get_2D
targ_idxs,count = obj[2], obj[3]
result = []
@ -58,7 +57,7 @@ class Index2DHost():
np.random.shuffle(ar)
sub_idxs.append(ar.pop())
result.append (sub_idxs)
cqs[cq_id].put (result)
self.cqs[cq_id].put (result)
time.sleep(0.005)
@ -100,19 +99,18 @@ class IndexHost():
"""
Provides random shuffled indexes for multiprocesses
"""
def __init__(self, indexes_count, max_number_of_clis=128):
def __init__(self, indexes_count):
self.sq = multiprocessing.Queue()
self.cqs = [ multiprocessing.Queue() for _ in range(max_number_of_clis) ]
self.n_clis = 0
self.max_number_of_clis = max_number_of_clis
self.cqs = []
self.clis = []
self.thread = threading.Thread(target=self.host_thread, args=(indexes_count,) )
self.thread.daemon = True
self.thread.start()
self.p = multiprocessing.Process(target=self.host_proc, args=(indexes_count, self.sq, self.cqs) )
self.p.daemon = True
self.p.start()
def host_proc(self, indexes_count, sq, cqs):
def host_thread(self, indexes_count):
idxs = [*range(indexes_count)]
shuffle_idxs = []
sq = self.sq
while True:
while not sq.empty():
@ -125,18 +123,15 @@ class IndexHost():
shuffle_idxs = idxs.copy()
np.random.shuffle(shuffle_idxs)
result.append(shuffle_idxs.pop())
cqs[cq_id].put (result)
self.cqs[cq_id].put (result)
time.sleep(0.005)
def create_cli(self):
if self.n_clis == self.max_number_of_clis:
raise Exception("")
cq_id = self.n_clis
self.n_clis += 1
return IndexHost.Cli(self.sq, self.cqs[cq_id], cq_id)
cq = multiprocessing.Queue()
self.cqs.append ( cq )
cq_id = len(self.cqs)-1
return IndexHost.Cli(self.sq, cq, cq_id)
# disable pickling
def __getstate__(self):
@ -159,50 +154,50 @@ class IndexHost():
time.sleep(0.001)
class ListHost():
def __init__(self, list_=None, max_number_of_clis=128):
def __init__(self, list_):
self.sq = multiprocessing.Queue()
self.cqs = [ multiprocessing.Queue() for _ in range(max_number_of_clis) ]
self.n_clis = 0
self.max_number_of_clis = max_number_of_clis
self.p = multiprocessing.Process(target=self.host_proc, args=(self.sq, self.cqs) )
self.p.daemon = True
self.p.start()
def host_proc(self, sq, cqs):
m_list = list()
self.cqs = []
self.clis = []
self.m_list = list_
self.thread = threading.Thread(target=self.host_thread)
self.thread.daemon = True
self.thread.start()
def host_thread(self):
sq = self.sq
while True:
while not sq.empty():
obj = sq.get()
cq_id, cmd = obj[0], obj[1]
if cmd == 0:
cqs[cq_id].put ( len(m_list) )
self.cqs[cq_id].put ( len(self.m_list) )
elif cmd == 1:
idx = obj[2]
item = m_list[idx ]
cqs[cq_id].put ( item )
item = self.m_list[idx ]
self.cqs[cq_id].put ( item )
elif cmd == 2:
result = []
for item in obj[2]:
result.append ( m_list[item] )
cqs[cq_id].put ( result )
result.append ( self.m_list[item] )
self.cqs[cq_id].put ( result )
elif cmd == 3:
m_list.insert(obj[2], obj[3])
self.m_list.insert(obj[2], obj[3])
elif cmd == 4:
m_list.append(obj[2])
self.m_list.append(obj[2])
elif cmd == 5:
m_list.extend(obj[2])
self.m_list.extend(obj[2])
time.sleep(0.005)
def create_cli(self):
if self.n_clis == self.max_number_of_clis:
raise Exception("")
cq = multiprocessing.Queue()
self.cqs.append ( cq )
cq_id = len(self.cqs)-1
return ListHost.Cli(self.sq, cq, cq_id)
cq_id = self.n_clis
self.n_clis += 1
return ListHost.Cli(self.sq, self.cqs[cq_id], cq_id)
def get_list(self):
return self.list_
# disable pickling
def __getstate__(self):