0%
简介
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import multiprocessing import json,time
class Work(object): def __init__(self,q): self.q = q def send(self,message): if not isinstance(message,str): message = json.dumps(message) self.q.put(message) def send_all(self): for i in range(20): self.q.put(i) time.sleep(1)
def receive(self): while 1: result = self.q.get() try: res = json.loads(result) except: res = result print('recv is %s' % res)
if __name__=='__main__': q = multiprocessing.Queue() work = Work(q) send = multiprocessing.Process(target=work.send,args=({'name':111},)) recv = multiprocessing.Process(target=work.receive) send_all_p = multiprocessing.Process(target=work.send_all)
send_all_p.start() send.start() recv.start() send_all_p.join() recv.terminate()
|