import time
import threading
import queue as Queue
def f(q):
i = 0
while True:
q.put(i)
print('put', i)
time.sleep(0.5)
i += 1
def g(l, q):
while True:
time.sleep(5)
l.acquire()
while not q.empty():
print(q.get())
time.sleep(1)
time.sleep(5)
l.release()
Q = Queue.Queue()
l = threading.Lock()
t0 = threading.Thread(target=f, args=[Q])
t0.start()
t1 = threading.Thread(target=g, args=[l, Q])
t1.start()
aW1wb3J0IHRpbWUKaW1wb3J0IHRocmVhZGluZwppbXBvcnQgcXVldWUgYXMgUXVldWUKCgpkZWYgZihxKToKICAgIGkgPSAwCiAgICB3aGlsZSBUcnVlOgogICAgICAgIHEucHV0KGkpCiAgICAgICAgcHJpbnQoJ3B1dCcsIGkpCiAgICAgICAgdGltZS5zbGVlcCgwLjUpCiAgICAgICAgaSArPSAxCgpkZWYgZyhsLCBxKToKICAgIHdoaWxlIFRydWU6CiAgICAgICAgdGltZS5zbGVlcCg1KQogICAgICAgIGwuYWNxdWlyZSgpCiAgICAgICAgd2hpbGUgbm90IHEuZW1wdHkoKToKICAgICAgICAgICAgcHJpbnQocS5nZXQoKSkKICAgICAgICAgICAgdGltZS5zbGVlcCgxKQogICAgICAgIHRpbWUuc2xlZXAoNSkKICAgICAgICBsLnJlbGVhc2UoKQoKUSA9IFF1ZXVlLlF1ZXVlKCkKbCA9IHRocmVhZGluZy5Mb2NrKCkKdDAgPSB0aHJlYWRpbmcuVGhyZWFkKHRhcmdldD1mLCBhcmdzPVtRXSkKdDAuc3RhcnQoKQp0MSA9IHRocmVhZGluZy5UaHJlYWQodGFyZ2V0PWcsIGFyZ3M9W2wsIFFdKQp0MS5zdGFydCgp