python 用线程 Queue时,有个线程没工作感觉,咋回事

色郎中 2017-04-01 11:58:34
再学习python时,网上找了段代码,作了些修改
前两天刚弄完时,程序还正常的,生产者和消费者确打印信息出来的,只是,消费者开5个线程时,只有一个线程能打印消息


今天再跑时,发现更不正常了
一个生产者,一个消费者,程序跑起来后,消费者线程的信息没有打印出来(在消费者线程里,断点时,能打印一次信息出来)



class consumer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.setName("new" + self.name)

def run(self):
global queue
while queue.qsize() > 0:
msg = self.name + ' consumer ' + queue.get()+ "get Queue Size: "+ str(queue.qsize())
print msg
time.sleep(5)

class produce(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.setName("new"+self.name)

def run(self):
global queue
b = os.path.exists(".\\testFile\\")
if b:
print "File direction Exist!"
else:
os.mkdir(".\\testFile\\")
while True:
for i in range(1, 1 + 1):
localTime = time.strftime("%Y%m%d%H%M%S", time.localtime())
filename = ".\\testFile\\" + localTime + ".txt"
f = open(filename, 'ab')
testnote = 'test file'
f.write(testnote)
f.close()
queue.put(filename)
print "file" + " " + str(i) + ":" + str(localTime) + ".txt"+ "put Queue Size: "+ str(queue.qsize())
time.sleep(1)
#print "ALL Down"
time.sleep(1)



def threadTest():
p = produce()
c = consumer()
p.start()
c.start()
p.join()
c.join()


if __name__ == '__main__':
threadTest()



结果如下:

C:\Python27\python.exe E:/2017/queueExample/queueExample.py
File direction Exist!
file 1:20170401113753.txtput Queue Size: 1
file 1:20170401113755.txtput Queue Size: 2
file 1:20170401113757.txtput Queue Size: 3
file 1:20170401113759.txtput Queue Size: 4
file 1:20170401113801.txtput Queue Size: 5



...全文
165 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
色郎中 2017-04-01
  • 打赏
  • 举报
回复

class consumer(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.setName("new" + self.name)
 
    def run(self):
        global queue
        while queue.qsize() > 0:
            msg = self.name + '    consumer    ' + queue.get()+ "get Queue Size: "+ str(queue.qsize())
            print msg
            time.sleep(5)
应该是 while queue.qsize() > 0 这句出了问题, 估计是线程都同时,启动了,消费者里的 queue的大小 为0了,所以没打印消息出来
色郎中 2017-04-01
  • 打赏
  • 举报
回复
又把网上例子跑了下,正常 我修改的代码,区别 1 修改的代码里,线程中 一直产生文件, 网上例子做5次 2 Queqe,没有作为参数传入, 而是作为全局变量使用,难道这个会影响?

# producer_consumer_queue

from Queue import Queue

import random

import threading

import time


# Producer thread

class Producer(threading.Thread):
    def __init__(self, t_name, queue):
        threading.Thread.__init__(self, name=t_name)

        self.data = queue

    def run(self):
        for i in range(5):
            print "%s: %s is producing %d to the queue!\n" % (time.ctime(), self.getName(), i)

            self.data.put(i)

            time.sleep(random.randrange(10) / 5)

        print "%s: %s finished!" % (time.ctime(), self.getName())



        # Consumer thread


class Consumer(threading.Thread):
    def __init__(self, t_name, queue):
        threading.Thread.__init__(self, name=t_name)

        self.data = queue

    def run(self):
        for i in range(5):
            val = self.data.get()

            print "%s: %s is consuming. %d in the queue is consumed!\n" % (time.ctime(), self.getName(), val)

            time.sleep(random.randrange(10))

        print "%s: %s finished!" % (time.ctime(), self.getName())



        # Main thread


def main():
    queue = Queue()

    producer = Producer('Pro.', queue)

    consumer = Consumer('Con.', queue)

    producer.start()

    consumer.start()

    producer.join()

    consumer.join()

    print 'All threads terminate!'


if __name__ == '__main__':
    main()

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧