|
- from threading import Thread
- import multiprocessing as mp
- import time
- import random
- from queue import Queue
-
- inputQueue = mp.JoinableQueue()
- inputQueue2 = mp.Queue(10)
-
- class ProducerThread(Thread):
- def __init__(self,name='producer thread',custom_id=0,daemon=False):
- super(ProducerThread, self).__init__()
- self.name = "{}-{}".format(name,custom_id)
- self.setDaemon(daemon)
-
- #def threadjoin(self):
- # super(ProducerThread,self).join()
-
- global inputQueue
- def run(self):
- numbers = range(10)
- counter = 0
- while counter < 20:
- num = random.choice(numbers)
- inputQueue.put(num)
- print("\nPut",num)
- time.sleep(0.5)
- counter+=1
-
-
-
- class ConsumerProcess1(mp.Process):
- def __init__(self,name='consumer thread',custom_id=0,daemon=False):
- super(ConsumerProcess1,self).__init__()
- self.name = "{}-{}".format(name,custom_id)
- self.daemon = daemon
-
- #def consumerjoin(self):
- # super(ConsumerProcess1,self).join()
- # return
-
- global inputQueue
- def run(self):
- while (not inputQueue.empty()):
- num = inputQueue.get()
- inputQueue.task_done()
- print("\nGot", num)
- time.sleep(1)
-
-
- if __name__ == "__main__":
-
- print("this is example 2")
- time.sleep(2)
- a=ProducerThread(name = 'producer thread',custom_id=1,daemon=False)
- b=ConsumerProcess1(name = 'consumer process',custom_id=1,daemon=True)
-
- a.start()
- b.start()
-
- print(a.ident,a.name,"\n")
- print(b.ident,b.name,"\n")
-
- a.join()
- b.join()
-
- print('end of program mark')
|