def main(q=None): """ Try to detect relics on the screen and show if they can be detected in a window. For some weird reason, Qt and tesseract cannot run in the same thread. Doing so leads to a crash and I'm unable to figure out the cause. We workaround this by creating a thread for Qt and putting all ocr result into a queue. :param q: A queue for communication with the Qt thread """ if q is not None: app = QApplication(sys.argv) widget = Widget(q) sys.exit(app.exec_()) q = Queue(1) p = Process(target=main, args=(q, )) p.start() tessdata_dir = 'tessdata/' with TesserocrPool(tessdata_dir, 'Roboto', psm=PSM.SINGLE_BLOCK, oem=OEM.LSTM_ONLY) as pool, mss.mss() as sct: s = Screenshots(sct) while p.is_alive(): begin = time.time() image_input = next(s) end = time.time() delta = end - begin print(f'screenshot took {delta}s') try: ocr_data = do_ocr(pool, image_input) except: ocr_data = None if ocr_data is None: ocr_data = itertools.repeat(('ocrerror', ) * 4, 20) try: q.put(tuple(ocr_data), block=True, timeout=0.5) except QueueFullException: if not p.is_alive(): break except ValueError as e: pass except AssertionError as e: pass p.join()
def main(): """ Creates instances of the above methods and occassionally checks for crashed worker processes & relaunches. """ worker_process = list() get_update_process = Process(target=get_updates) get_update_process.start() for i in range(0, int(CONFIG['BOT_CONFIG']['workers'])): worker_process.append(Process(target=process_updates)) worker_process[i].start() time_worker = ThreadProcess(target=check_time_args) time_worker.start() while RUNNING.value: time.sleep(30) for index, worker in enumerate(worker_process): if not worker.is_alive(): del worker_process[index] worker_process.append(Process(target=process_updates)) worker_process[-1].start() if not time_worker.is_alive(): time_worker = ThreadProcess(target=check_time_args) time_worker.start() if not get_update_process.is_alive(): get_update_process = Process(target=get_updates) get_update_process.start() get_update_process.join() time_worker.join() for worker in worker_process: worker.join()
def calc_nrst_dist_cpu(gids,xs,ys,densities,cpu_core): n=xs.shape[0] def calc_nrst_dist_np(gidxys,result_q,gids,xs,ys,densities): while True: try: i=gidxys.get_nowait() distpow2=(xs-xs[i])**2+(ys-ys[i])**2 distpow2[densities<=densities[i]]=1e100 pg=distpow2.argsort()[0] if distpow2[pg]>1e99: result_q.put((i,1e10,-1)) else: result_q.put((i,math.sqrt(distpow2[pg]),gids[pg])) except queue.Empty: break; n=xs.shape[0] gidxys=queue.Queue() result_q=queue.Queue() for i in range(n): gidxys.put(i) arcpy.SetProgressor("step", "Find Point with Higher Density on CPU...",0, n, 1) ts=[] for i in range(cpu_core): t=Process(target=calc_nrst_dist_np,args=(gidxys,result_q,gids,xs,ys,densities)) t.start() ts.append(t) for t in ts: while t.is_alive(): arcpy.SetProgressorPosition(n-gidxys.qsize()) time.sleep(0.05) result_a=[] while result_q.empty()==False: result_a.append(result_q.get()) result_a.sort() result_nd=[] result_pg=[] for v in result_a: result_nd.append(v[1]) result_pg.append(v[2]) return (np.array(result_nd),np.array(result_pg))
def calc_density_cpu(xs,ys,weights,kernel_type,cpu_core,cutoffd=0,sigma=0): xs=xs-xs.min() ys=ys-ys.min() def calc_density_np(gidxys,result_q,xs,ys,weights,kernel_type,cutoffd=0,sigma=0): while True: try: i=gidxys.get_nowait() distpow2=(xs-xs[i])**2+(ys-ys[i])**2 if kernel_type=='GAUSS': result_q.put( (i,((distpow2<((3*sigma)**2))*np.exp(-distpow2/(sigma**2))*weights).sum())) else: result_q.put( (i,((distpow2<(cutoffd**2))*weights).sum())) except queue.Empty: break; n=xs.shape[0] gidxys=queue.Queue() result_q=queue.Queue() for i in range(n): gidxys.put(i) arcpy.SetProgressor("step", "Calculate Densities on CPU...",0, n, 1) ts=[] for i in range(cpu_core): t=Process(target=calc_density_np,args=(gidxys,result_q,xs,ys,weights,kernel_type,cutoffd,sigma)) t.start() ts.append(t) for t in ts: while t.is_alive(): arcpy.SetProgressorPosition(n-gidxys.qsize()) time.sleep(0.05) result_a=[] while result_q.empty()==False: result_a.append(result_q.get()) result_a.sort() result_d=[] for v in result_a: result_d.append(v[1]) return np.array(result_d)
class RaftDaemon(object): def __init__(self): raftos.configure({ 'log_path': '/tmp', 'serializer': raftos.serializers.JSONSerializer }) self.conf = common.CLUSTER_CONF self.clusters = [ '{}:{}'.format(cluster, self.conf.get('raft_port')) for cluster in self.conf.get('raft_cluster_machines') if cluster != common.vip.ip ] self.node = '{}:{}'.format(common.vip.ip, self.conf.get('raft_port')) self.raft_thread = None self.monitor_thread = None def start_raft(self): common.raft_status.clear() common.vip_status.clear() common.raft_restart.clear() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.create_task(raftos.register(self.node, cluster=self.clusters)) loop.run_until_complete(self.run_node()) def start(self): self.raft_thread = Thread(target=self.start_raft, args=()) self.raft_thread.daemon = True self.raft_thread.start() self.monitor_thread = Thread(target=self.run_monitor, args=()) # self.monitor_thread.daemon = True self.monitor_thread.start() pass def run_monitor(self): # while True: # if not self.thread.is_alive() or common.raft_restart.wait(0): # tools.logger.info('raft daemon restarting... ') # self.restart() while True: print(self.raft_thread.getName(), self.raft_thread.is_alive()) time.sleep(5) stop_thread(self.raft_thread) self.raft_thread = Thread(target=self.start_raft, args=()) self.raft_thread.daemon = True self.raft_thread.start() async def run_node(self): data_id = raftos.Replicated(name='data_id') data = raftos.ReplicatedDict(name='data') data_list = raftos.ReplicatedList(name='data_list') while True: await raftos.wait_until_leader(self.node) await asyncio.sleep(2) current_id = random.randint(1, 1000) data_map = { str(current_id): { 'created_at': datetime.datetime.now().strftime('%d/%m/%y %H:%M') } } await data_id.set(current_id) await data.update(data_map) await data_list.append(data_map)
def dens_filter_cpu(cls_input,cntr_input,id_field,cntr_id_field,dens_field,cls_output,dist_thrs,dens_thrs,cpu_core): fieldsarray=[f.name for f in arcpy.Describe(cls_input).fields if f.type!='Geometry'] fieldsarray+=['SHAPE@X','SHAPE@Y'] arrays=arcpy.da.FeatureClassToNumPyArray(cls_input,fieldsarray) arrays=arrays[arrays[dens_field]>=dens_thrs] centerids=arcpy.da.FeatureClassToNumPyArray(cntr_input,[id_field]) arcpy.SetProgressorPosition(1) centers_q=queue.Queue() results_q=queue.Queue() for i in centerids: centers_q.put(i[0]) def filterbycenter(centers_q,results_q,arrays,cntr_id_field,id_field): while True: try: center_id=centers_q.get_nowait() cls_a=arrays[arrays[cntr_id_field]==center_id].copy() cls_q=queue.Queue() cls_q.put(center_id) cls_set=set([center_id]) while not cls_q.empty(): c_p_id=cls_q.get_nowait() c_p=cls_a[cls_a[id_field]==c_p_id] x=c_p['SHAPE@X'][0] y=c_p['SHAPE@Y'][0] distpow2_a=(cls_a['SHAPE@X']-x)**2+(cls_a['SHAPE@Y']-y)**2 near_p_id=cls_a[distpow2_a<dist_thrs**2][id_field] for i in near_p_id: if i not in cls_set: cls_q.put(i) cls_set.add(i) results_q.put(i) del cls_q,cls_a,cls_set except queue.Empty: break; arcpy.SetProgressor("step", "Density Filtering Points...",0, arrays.shape[0], 1) ts=[] for i in range(cpu_core): t=Process(target=filterbycenter,args=(centers_q,results_q,arrays,cntr_id_field,id_field)) t.start() ts.append(t) for t in ts: while t.is_alive(): arcpy.SetProgressorPosition(centerids.shape[0]-centers_q.qsize()) time.sleep(0.05) results_set=set() while not results_q.empty(): results_set.add(results_q.get_nowait()) results_a=[] for i in range(arrays.shape[0]): if arrays[id_field][i] in results_set: results_a.append(arrays[i]) # if '64 bit' in sys.version and id_field==arcpy.Describe(cls_input).OIDFieldName: # sadnl=list(arrays.dtype.names) # sadnl[sadnl.index(id_field)]='OID@' # arrays.dtype.names=tuple(sadnl) arcpy.da.NumPyArrayToFeatureClass(np.array(results_a,arrays.dtype),cls_output,\ ('SHAPE@X','SHAPE@Y'),arcpy.Describe(cls_input).spatialReference) return
import sys from multiprocessing.dummy import Process import time def printHello(numTimes): print("Hello " + str(numTimes)) def delay(sec): for x in range(sec): time.sleep(1) # print("Delay one second") p = Process(target=delay, args=(5, )) p.start() print("Process alive: " + str(p.is_alive())) map(printHello, range(20)) while (p.is_alive()): pass print("Exit code: " + str(p.exitcode))