Skip to content

Instantly share code, notes, and snippets.

@friskfly
Created January 3, 2013 11:19
Show Gist options
  • Save friskfly/4442749 to your computer and use it in GitHub Desktop.
Save friskfly/4442749 to your computer and use it in GitHub Desktop.
python 模拟银行排队取号系统 语音播报
#coding:utf-8
'''
@author:FX
2013-1-3
'''
import random
import threading
import time
import Queue
import speech
Number = 0;
def machine():
global Number;
Number+=1
return Number
lock = threading.Lock() #取号机一次只能取出一个
class Bank(threading.Thread):
"""银行Bank,消费者"""
def __init__(self,t_name,queue):
threading.Thread.__init__(self,name=t_name)
self.data = queue
def run(self):
while self.data.qsize() > 0:
consumer = self.data.get()
say="请"+str(consumer)+"号顾客到"+self.getName()+"号窗口"
speech.say(say.decode('utf8'))
time.sleep(1)
print u"%s : %s 正在接待 %s" %(time.ctime(), self.getName(), consumer)
say = self.getName()+"号柜员正在接待"+str(consumer) +"号顾客!"
speech.say(say.decode('utf8'))
time.sleep(random.randint(1,20))
print u"%s 号完成业务办理!" %(consumer)
say = str(consumer) + "号顾客完成业务办理,欢迎再次光临!"
speech.say(say.decode('utf8'))
class Consumer(threading.Thread):
"""顾客Consumer,生产者"""
def __init__(self,queue):
threading.Thread.__init__(self)
self.data = queue
def run(self):
if lock.acquire():
self.data.put(machine())
lock.release()
if __name__=='__main__':
queue=Queue.Queue()
print u"银行排队系统开始"
for i in range(5):
thread = Consumer(queue)
thread.start()
cabinetPool=[]
for i in range(1,5):
thread = Bank(str(i),queue)
thread.start()
cabinetPool.append(thread)
for thread in cabinetPool:
thread.join()
print u"任务全部结束"
speech.say(u"任务全部结束")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment