不要每天都kill进程,python消息队列服务退出还有什么更好的方法?,killpython,最近用python处理一
不要每天都kill进程,python消息队列服务退出还有什么更好的方法?,killpython,最近用python处理一
最近用python处理一个消息队列的问题 所有功能都完善了 到现在还有一个没有解决的问题就是 当服务打开后 无法使用ctrl+c 或者发送结束的single结束 每次都要kill进程号才可以 不知道有没有更好的办法
消息队列结构代码如下:对线程其实还是有点迷糊的 指导下 谢谢。
#encoding=utf-8import threadingimport randomimport timefrom Queue import Queueclass Producer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): while True: for i in range(20): print self.getName(),'adding',i,'to queue' self.sharedata.put(i) time.sleep(random.randrange(10)/10.0) time.sleep(8) print '======== NEW ===========' print self.getName(),'Finished'# Consumer threadclass Consumer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): while True: print self.getName(),'got a value:',self.sharedata.get() time.sleep(random.randrange(10)/10.0) print self.getName(),'Finished'# Main threaddef main(): queue = Queue() producer = Producer('Producer', queue) consumer = Consumer('Consumer', queue) print 'Starting threads ...' producer.start() consumer.start() producer.join() consumer.join() print 'All threads have terminated.'if __name__ == '__main__': main()
http://docs.python.org/library/signal...
文档及大部分教程的方法是将线程设为后台线程setDaemon(True),这样当队列为空时,没有线程join,就会自动退出
编橙之家文章,
相关内容
- 大学毕业选择python还是java更有前景,pythonjava,新年伊始
- 关于python 累加变量有些不解之处求答案,python累加,
- Python webpy中AttributeError是什么类型错误,,这是什么错误
- 想要查看flask输出jinja模板操作方法是怎么样的?,fl
- python如何控制输出汉字数量个数,python汉字数量个数
- fabric SSH keyfile登录远程服务器需要密码是什么情况,
- Python Scrapy爬不出数据源码哪里有问题?,pythonscrapy,我在
- Python struct.pack如何理解其原理,pythonstruct.pack,def setUT
- python weakly-referenced object no longer exists? 数据库连接关闭
- Python输出内容时取消换行如何操作?一行显示,python换行
评论关闭