>>> 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
>>>