糖果机和信号量(candy.py)

>>> import sys
>>> version
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    version
NameError: name 'version' is not defined
>>> print(sys.version)
3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)]
>>> 
#!/usr/bin/env python

from atexit import register
from random import randrange
from threading import BoundedSemaphore, Lock, Thread
from time import sleep, ctime

lock = Lock()
MAX = 5
candytray = BoundedSemaphore(MAX)

def refill():
    lock.acquire()
    print('Refilling candy...')
    try:
        candytray.release()
    except ValueError:
        print("Full skipping")
    else:
        print("OK")
    lock.release()
def buy():
    lock.acquire()
    print('Buying candy...')
    if candytray.acquire(False):
        print("OK")
    else:
        print("Empty, skipping")
    lock.release()

def producer(loops):
    for i in xrange(loops):
        refill()
        sleep(randrange(3))

def consumer(loops):
    for i in xrange(loops):
        buy()
        sleep(randrange(3))

def _main():
    print("starting at: ", ctime())
    nloops = randrange(2,6)
    print("The candy marchine (full with %d bars)!" % MAX)
    Thread(target=consumer, args=(randrange(
        nloops, nloops+MAX+2),)).start() #buyer
    Thread(target=producer, args=(nloops, )).start() #vndr

@register
def _atexit():
    print("All done at: " % ctime())

if __name__ == '__main__':
    _main()
    

Report the below error

= RESTART: C:/Users/hanwuwex/AppData/Local/Programs/Python/Python36/candy.py =
starting at:  Fri Apr 19 16:41:57 2019
The candy marchine (full with 5 bars)!
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\hanwuwex\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\hanwuwex\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/hanwuwex/AppData/Local/Programs/Python/Python36/candy.py", line 37, in consumer
    for i in xrange(loops):
NameError: name 'xrange' is not defined
Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\hanwuwex\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\hanwuwex\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/hanwuwex/AppData/Local/Programs/Python/Python36/candy.py", line 32, in producer
    for i in xrange(loops):
NameError: name 'xrange' is not defined
>>> 

KeyboardInterrupt
>>> 

If we want to modify this error, we need use range to replace xrange function, because xrange is not support in python 3.x

After replace xrange, the reasult like the follow shows:

= RESTART: C:/Users/hanwuwex/AppData/Local/Programs/Python/Python36/candy.py =
starting at:  Fri Apr 19 16:50:57 2019
The candy marchine (full with 5 bars)!
Buying candy...
>>> 
OK
Refilling candy...
OK
Refilling candy...
Full skipping
Refilling candy...
Full skipping
Refilling candy...
Full skipping
Buying candy...
OK
Refilling candy...
OK
Buying candy...
OK
Buying candy...
OK
Buying candy...
OK
Buying candy...
OK
Buying candy...
OK
Buying candy...
Empty, skipping

>>> 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值