Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created December 9, 2013 03:26
Show Gist options
  • Save yao2030/7866981 to your computer and use it in GitHub Desktop.
Save yao2030/7866981 to your computer and use it in GitHub Desktop.
# -*- coding: UTF-8 -*-
import time
from Queue import Queue
from Objs import *
BATAI_PROC_TIME = 10
MAX_BATAI_NUM = 20
SHAOKAO_PROC_TIME = 180
MAX_SHAOKAO_NUM = 8
def question4() :
people_count = 25
interval = 60
queue1 = Queue(100)
queue2 = Queue(100)
mydoor = door(people_count, interval, queue1)
mydoor.start()
print "start question 4 ..."
start_time = time.time();
for i in range (MAX_BATAI_NUM) :
mybt = batai(BATAI_PROC_TIME, i, queue1, queue2, mydoor)
mybt.start()
for i in range (MAX_SHAOKAO_NUM) :
myskj = shaokaojia(SHAOKAO_PROC_TIME, i, queue2, mydoor)
myskj.start()
mydoor.join()
end_time = time.time()
sub_time = end_time - start_time
print "end questio 4 time:%d" % (sub_time)
def question1() :
people_count = 1
interval = 0
queue1 = Queue(100)
queue2 = Queue(100)
mydoor = door(people_count, interval, queue1)
mydoor.start()
print "start question 1 ..."
start_time = time.time();
for i in range (MAX_BATAI_NUM) :
mybt = batai(BATAI_PROC_TIME, i, queue1, queue2, mydoor)
mybt.start()
for i in range (MAX_SHAOKAO_NUM) :
myskj = shaokaojia(SHAOKAO_PROC_TIME, i, queue2, mydoor)
myskj.start()
mydoor.join()
end_time = time.time()
sub_time = end_time - start_time
print "end questio 1 time:%d" % (sub_time)
if __name__ == "__main__" :
start_time = time.time();
print "start app ..."
question1()
question4()
end_time = time.time();
sub_time = end_time - start_time
print "all time:%d" % (sub_time)
print "end app ."
# -*- coding: UTF-8 -*-
import os
import sys
from Queue import Queue
import random
import threading
import time
MAX_CAI_LIAO = 5
CAI_LIAO_FULL = 1
CAI_LIAO_NOT_FULL = 0
MAX_SHAOKAOJIA = 8
class customer :
def __init__(self, id):
self.c_id = id
self.cailiao_all = CAI_LIAO_NOT_FULL
self.cailiao = []
self.can_eat = 0;
def add_cailiao(self) :
if(self.cailiao_all == 0) :
self.cailiao.append(1)
if(len(self.cailiao) == MAX_CAI_LIAO) :
self.cailiao_all = CAI_LIAO_FULL
def is_cailiao_all(self) :
return self.cailiao_all
def set_can_eat(self) :
self.can_eat = 1
def is_can_eat(self) :
return self.can_eat
class batai(threading.Thread) :
def __init__(self, process_time, id, in_queue, out_queue, doorObj):
threading.Thread.__init__(self, name="bt_%d" % (id))
self.proc_time = process_time
self.in_data = in_queue
self.out_data = out_queue
self.door = doorObj
def run(self) : # get cailiao
while True :
try :
val = self.in_data.get(0)
val.add_cailiao()
except :
if (self.door.is_finish()) :
break
time.sleep(1)
continue
print "customer:%d, bt:%s, add cailiao:%d, full:%d" % (
val.c_id, self.name, len(val.cailiao), val.is_cailiao_all())
time.sleep(self.proc_time);
if(val.is_cailiao_all() == CAI_LIAO_FULL) :
self.door.add_selected()
self.out_data.put(val)
else :
self.in_data.put(val)
class shaokaojia(threading.Thread) :
def __init__(self, process_time, id, in_queue, doorObj) :
threading.Thread.__init__(self, name="skj_%d" % (id))
self.proc_time = process_time
self.in_data = in_queue
self.door = doorObj
def run(self) : # kao
while True :
try :
val = self.in_data.get(0)
except :
if (self.door.is_finish()) :
break
time.sleep(1)
continue
time.sleep(self.proc_time)
val.set_can_eat()
print "customer:%d, kao:%s and eat:%d" % (
val.c_id, self.name, val.is_can_eat())
self.door.add_eat()
mylock_1 = threading.RLock();
mylock_2 = threading.RLock();
class door(threading.Thread) :
def __init__(self, number, interval, out_queue) :
threading.Thread.__init__(self, name="d_%d" % (number))
self.number = number
self.interval = interval
self.out_data = out_queue
self.selected_num = 0
self.eat_num = 0
self.door_finish = False
def run(self) : # create people
last = self.number
while last > 0 :
p = customer(self.number-last + 1)
last = last - 1
print "customer %d come in ...\n" % (p.c_id)
self.out_data.put(p)
time.sleep(self.interval)
while True :
if(self.selected_num == self.number and self.eat_num == self.number) :
self.door_finish = True
break;
else :
time.sleep(1)
def is_finish(self) :
return self.door_finish
def add_selected(self) :
mylock_1.acquire()
if(self.selected_num < self.number) :
self.selected_num = self.selected_num + 1
mylock_1.release()
def add_eat(self) :
mylock_2.acquire()
if(self.eat_num < self.number) :
self.eat_num = self.eat_num + 1
mylock_2.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment