本文整理汇总了Python中timer.Timer.get_waittime方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.get_waittime方法的具体用法?Python Timer.get_waittime怎么用?Python Timer.get_waittime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类timer.Timer
的用法示例。
在下文中一共展示了Timer.get_waittime方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Philosopher
# 需要导入模块: from timer import Timer [as 别名]
# 或者: from timer.Timer import get_waittime [as 别名]
class Philosopher(Thread):
def __init__(self, id, run_time=RUN_TIME):
Thread.__init__(self)
# Init class variables
self.id = id
self.left_chopstick = chopsticks[id]
self.right_chopstick = chopsticks[(id + 1) % NUM_PHILOSOPHERS]
self.stop_time = time.time() + run_time
self.feedings = 0
self.chopstick_timer = Timer()
def run(self):
""" Called when the thread is started. Keeps trying to eat
until it's time to stop """
while time.time() < self.stop_time:
self.chopstick_timer.start()
try:
self.think()
if time.time() >= self.stop_time: break
self.acquire_chopsticks() # may raise ChopsticksBusy
self.chopstick_timer.stop()
self.eat()
self.release_chopsticks()
except ChopsticksBusy:
pass
def think(self):
""" Philosophers like to think in between feedings """
#print "philosopher %d thinking" % self.id
wait(4)
def eat(self):
""" Mmmm.... food """
self.feedings = self.feedings + 1
#print "philosopher %d on feeding #%d" % (self.id, self.feedings)
wait(2)
def acquire_chopsticks(self):
""" Acquires the chopsticks. """
self.left_chopstick.acquire()
# We don't want to hold on to the left chopstick
# forever, so we release it and try again
if not self.right_chopstick.acquire(0):
self.left_chopstick.release()
raise ChopsticksBusy
def release_chopsticks(self):
""" Releases both chopsticks """
self.left_chopstick.release()
self.right_chopstick.release()
def get_waittime(self):
""" Returns the total amount of time the philosopher
has waited """
return self.chopstick_timer.get_waittime()
def get_feedings(self):
""" Returns the total amount of feedings """
return self.feedings
示例2: range
# 需要导入模块: from timer import Timer [as 别名]
# 或者: from timer.Timer import get_waittime [as 别名]
chopsticks = []
for i in range(NUM_PHILOSOPHERS): # 1 chopstick for every philosopher
chopsticks.append(Lock())
timer1 = Timer()
timer1.start()
# Create the philosophers and get them feeding
philosophers = []
for i in range(NUM_PHILOSOPHERS):
p = Philosopher(i)
philosophers.append(p)
p.start()
# Wait for everyone to finish.
for p in philosophers:
p.join()
timer1.stop()
# Print all totals
print "\nTime each philosopher waited:"
for p in philosophers:
print "Philosopher %d wait time: %.2f seconds" % ( p.id, p.get_waittime() )
print "\nTotal time: %.2f seconds" % timer1.get_waittime()
print "\nNumber of times eaten: "
for p in philosophers:
print "Philosopher %d ate %d times" % ( p.id, p.get_feedings() )