python 多线程读写文件错误_python多线程老是报错。大神帮忙看看哈?
以下是代碼:importthreadingimporttimeproducts=[]condition=threading.Condition()classConsumer(threading.Thread):defconsume(self):globalconditionglobalproductsconditon.acqu...
以下是代碼:
import threading
import time
products=[]
condition = threading.Condition()
class Consumer(threading.Thread):
def consume(self):
global condition
global products
conditon.acquire()
if len(products)==0:
condition.wait()
print('消費提醒:沒有產品去消費了')
products.pop()
print('消費者提醒:消費1個產品')
print('消費者提醒:還能消費的產品數為'+str(len(products)))
condition.notify()
condition.release()
def run(self):
for i in range(0,20):
time.sleep(4)
self.consume()
class Producer(threading.Thread):
def produce(self):
global condition
global products
condition.acquire()
if len(products)==10:
condition.wait()
print('生產者提醒:生成的產品數為'+str(len(products)))
print('生產者提醒:停止生產!')
products.append(1)
print('生產者提醒:產品總數為'+str(len(products)))
condition.notify()
condition.release()
def run(self):
for i in range(0,20):
time.sleep(1)
self.produce()
producer=Producer()
consumer=Consumer()
consumer.start()
producer.start()
producer.join()
consumer.join()
報錯的提示:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/對對對.py", line 22, in run
self.consume()
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/對對對.py", line 10, in consume
conditon.acquire()
NameError: name 'conditon' is not defined
展開
總結
以上是生活随笔為你收集整理的python 多线程读写文件错误_python多线程老是报错。大神帮忙看看哈?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常见排序算法的时间复杂度汇总
- 下一篇: springmvc,spring,hib