import threading,time
import Queue
import random
q=Queue.Queue()
def Chan(name):
for i in range(5):
q.put(i)
print '%s shengchan %s baozi' %(name,i)
time.sleep(random.randrange(5))
def Chi(name):
count=0
while count < 5:
d=q.get()
print '%s chichichi %s baozi' %(name,d)
count +=1
time.sleep(random.randrange(5))
p=threading.Thread(target=Chan,args=('AAAAAAAAAAA',))
c=threading.Thread(target=Chi,args=('BBBBBBBBBBB',))
p.start()
c.start()
结果
AAAAAAAAAAA shengchan 0 baozi
BBBBBBBBBBB chichichi 0 baozi
AAAAAAAAAAA shengchan 1 baozi
AAAAAAAAAAA shengchan 2 baozi
BBBBBBBBBBB chichichi 1 baozi
AAAAAAAAAAA shengchan 3 baozi
BBBBBBBBBBB chichichi 2 baozi
AAAAAAAAAAA shengchan 4 baozi
BBBBBBBBBBB chichichi 3 baozi
BBBBBBBBBBB chichichi 4 baozi