Package: ipc-worker
ipc-worker: Inter-Process Communication , muti Process Woker works by share memory or MQ.supportsharememory(py>=3.8andlinux)andmqprocessworker(py>=3.6)# -*- coding: utf-8 -*-# @Time    : 2021/11/23 9:35'''demo share memroryrecommended system linux and python >= 3.8recommended linuxpython 3.8Do not recommended run in windows , it will report an error as followRuntimeError:An attempt has been made to start a new process before thecurrent process has finished its bootstrapping phase.'''importmultiprocessingimportosimportsignalfromipc_workerimportloggerfromipc_worker.ipc_shm_loaderimportIPC_shm,SHM_process_workerclassMy_worker(SHM_process_worker):def__init__(self,config,*args,**kwargs):super(My_worker,self).__init__(*args,**kwargs)#config info , use by yourselflogger.info('Process id{}, group name{},shm name{}'.format(self._idx,self._group_name,self._shm_name))logger.info(config)self.config=config#Process begin trigger this funcdefrun_begin(self):logger.info('worker pid{}...'.format(os.getpid()))self.handle=Nonepass# Process end trigger this funcdefrun_end(self):ifself.handleisnotNone:pass#any data put will trigger this funcdefrun_once(self,request_data):#process request_dataifisinstance(request_data,dict):request_data['b']=200ifself.handleisnotNone:#do some thingpassreturnrequest_dataif__name__=='__main__':config={"anything":"anything","aa":100}evt_quit=multiprocessing.Manager().Event()# group_name 为共享内存组名,需唯一# manager is an agent  and act as a load balancing# worker is real doing your workinstance=IPC_shm(CLS_worker=My_worker,worker_args=(config,),# must be tupleworker_num=10,# number of worker Processmanager_num=2,# number of agent Processgroup_name='serving_shm',# share memory nameshm_size=1*1024*1024,# share memory sizequeue_size=20,# recv queue sizeis_log_time=False,# whether log compute timedaemon=False,)instance.start()#demo produce and consume message , you can process by httpforiinrange(10):print('produce message')data={"a":100}request_id=instance.put(data)data=instance.get(request_id)print('get process result',data)defsignal_handler(signum,frame):evt_quit.set()instance.terminate()raiseKeyboardInterruptsignal.signal(signal.SIGINT,signal_handler)try:instance.join()exceptExceptionase:evt_quit.set()instance.terminate()delevt_quit# -*- coding: utf-8 -*-# @Time    : 2021/11/29 15:06# @Author  : tkimportmultiprocessingimportosimportsignalimporttorchfromipc_workerimportloggerfromipc_worker.ipc_zmq_loaderimportIPC_zmq,ZMQ_process_workerclassMy_worker(ZMQ_process_worker):def__init__(self,config,*args,**kwargs):super(My_worker,self).__init__(*args,**kwargs)# config info , use by yourselflogger.info('Process id{}, group name{}, identity{}'.format(self._idx,self._group_name,self._identity))logger.info(config)self.config=config# Process begin trigger this funcdefrun_begin(self):logger.info('worker pid{}...'.format(os.getpid()))self.handle=Nonepass# Process end trigger this funcdefrun_end(self):ifself.handleisnotNone:pass# any data put will trigger this funcdefrun_once(self,request_data):# process request_dataprint(torch.cuda.device_count(),torch.cuda.current_device())ifisinstance(request_data,dict):request_data['b']=200ifself.handleisnotNone:# do some thingpassreturnrequest_dataif__name__=='__main__':# torch.multiprocessing.set_start_method('spawn', force=True)'''demo ZMQ depend zmqpip install pyzmqtest pass >= python3.6'''tmp_dir='./tmp'ifnotos.path.exists(tmp_dir):os.mkdir(tmp_dir)os.environ['ZEROMQ_SOCK_TMP_DIR']=tmp_dirconfig={"anything":"anything","aa":100}evt_quit=multiprocessing.Manager().Event()# group_name 为共享内存组名,需唯一# manager is an agent  and act as a load balancing# worker is real doing your workinstance=IPC_zmq(CLS_worker=My_worker,worker_args=(config,),# must be tupleworker_num=10,# number of worker Processgroup_name='serving_zmq',# share memory nameevt_quit=evt_quit,queue_size=20,# recv queue sizeis_log_time=True,# whether log compute timedaemon=False,)instance.start()#demo produce and consume message , you can process by httpforiinrange(10):data={"a":100}request_id=instance.put(data)data=instance.get(request_id)print('get process result',request_id,data)defsignal_handler(signum,frame):evt_quit.set()instance.terminate()raiseKeyboardInterruptsignal.signal(signal.SIGINT,signal_handler)try:instance.join()exceptExceptionase:evt_quit.set()instance.terminate()delevt_quit