Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import threading
- import time
- BUFFEER_SIZE=10
- buffer=[]
- mutex=threading.Lock()
- notEmpty=threading.Semaphore(0)
- def producer():
- id = threading.current_thread().name
- print("producer : {}".format(id))
- for i in range(BUFFEER_SIZE):
- data = "{}i:{}".format(id,i)
- with mutex:
- buffer.append(data)
- notEmpty.release()
- def consumer():
- id=threading.current_thread().name
- for i in range(BUFFEER_SIZE):
- notEmpty.acquire()
- with mutex:
- data = buffer.pop()
- print("{} read: {}".format(id,data))
- st=time.time()
- thread=[]
- c=threading.Thread(target = consumer)
- thread.append(c)
- p=threading.Thread(target = producer)
- thread.append(p)
- for i in thread:
- i.start()
- for i in thread:
- i.join()
- edt=time.time()
- print("Time taken:",(edt-st))
Add Comment
Please, Sign In to add comment