python新手小白的多线程练习

多线程小练习
python新人小白,希望各位大佬多指点!

work1.py

import threading

class Buffer1(object):

    def __init__(self):
        # 缓冲区数据
        self.value = 0
        # 缓冲区状态
        self.empty = True # True缓冲区为空 False缓冲区为满
        # 多线程的状态
        self.con = threading.Condition()

    #线程1上传信息    
    def update1(self):
        self.con.aquire()
        name=threading.current_thread().name

        if self.empty:
            self.value=value
            self.empty=False
            print('%s运行中'%name)
            self.con.notifyAll()  #通知线程2可以运行了
        else:
            self.con.wait()  #线程2继续等待线程1完成



    #线程2下载信息
    def download2(self):
        self.con.acquire()
        name=threading.current_thread().name
        ret=0
        if self.empty:  #缓冲区为空
            self.con.wait()   #等待线程1运行完成
        else:
            ret=self.value
            self.empty=True
            print('%s运行中'%name)
            self.con.notifyAll()   #通知线程3可以运行了
        return ret



class Buffer2(object):

    #线程2上传信息
    def update2(self):  
        self.con.aquire()
        name=threading.current_thread().name

        if self.empty:
            self.value=value
            self.empty=False
            print('%s运行中'%name)
            self.con.notifyAll()  #通知线程3可以运行了
        else:
            self.con.wait()  #线程3继续等线程2

    #线程3下载信息
    def download3(self):
        self.con.acquire()
        name=threading.current_thread().name
        fei=0
        if self.empty:  #缓冲区为空
            self.con.wait()   #等待线程2运行完成
        else:
            fei=self.value
            self.empty=True
            print('%s运行中'%name)
            self.con.notifyAll()   #通知线程1可以运行了
        return fei

class Buffer3(object):
    #线程3上传信息
    def update2(self):  
        self.con.aquire()
        name=threading.current_thread().name

        if self.empty:
            self.value=value
            self.empty=False
            print('%s运行中'%name)
            self.con.notifyAll()  #通知线程1可以运行了
        else:
            self.con.wait()   #线程1等待

    #线程1下载信息
    def download1(self):
        self.con.acquire()
        name=threading.current_thread().name
        qiu=0
        if self.empty:  #缓冲区为空
            self.con.wait()   #等待线程2运行完成
        else:
            qiu=self.value
            self.empty=True
            print('%s运行中'%name)
            self.con.notifyAll()   #通知线程1可以运行了
        return qiu

work_main.py

import threading
from work1 import *
import time
import random


class MyThread1(threading.Thread):
    def __init__(self,work1,name):
        super().__init__()
        self.work1=work1
        self.name=name

    def run1(self):
        #不断的运行
        while True:
            time.sleep(1)

            ret=self.work1.download2(value)




class MyThread2(threading.Thread):
    def __init__(self,work1,name):
        super().__init__()
        self.work1=work1
        self.name=name

    def run1(self):
        while True:
            time.sleep(1)

            fei=self.work1.download3()

class MyThread3(threading.Thread):
    def __init__(self,work1,name):
        super().__init__()
        self.work1=work1
        self.name=name

    def run1(self):
        while True:
            time.sleep(1)

            qiu=self.work1.download1()


def main():
    work1=Buffer1
    work1=Buffer2
    work1=Buffer3
    thread1=MyThread1(work1,'线程1')
    thread1.start()
    thread2=MyThread2(work1,'线程2')
    thread2.start()
    thread3=MyThread3(work1,'线程3')
    thread3.start()



if __name__=='__main__':
    main()

运行了一遍发现运行不出来,我这是哪儿出问题了,,哇,也没报错,这很郁闷啊!!!
大伙儿有什么方案吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值