python 多线程应用
来源:互联网 发布:2015网络流行语 编辑:程序博客网 时间:2024/06/07 18:15
python 多线程应用
python可以方便地支持多线程。可以快速创建线程、互斥锁、信号量等等元素,支持线程读写同步互斥。美中不足的是,python的运行在python 虚拟机上,创建的多线程可能是虚拟的线程,需要由python虚拟机来轮询调度,这大大降低了python多线程的可用性。我们经今天用了经典的生产者和消费者的问题来说明下python的多线程的运用 上代码:
#encoding=utf-8
import threading
import random
import time
from Queue import Queue
class Producer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'adding',i,'to queue'
self.sharedata.put(i)
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Consumer thread
class Consumer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'got a value:',self.sharedata.get()
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Main thread
def 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()
import threading
import random
import time
from Queue import Queue
class Producer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'adding',i,'to queue'
self.sharedata.put(i)
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Consumer thread
class Consumer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'got a value:',self.sharedata.get()
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Main thread
def 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()
你亲自运行下这断代码,可能有不一样的感觉!理解以后可以用python cookielib 再结果python urllib 写一个多线程下载网页的脚本应该没什么问题了 作者:老王@python python教程 老王python,提供pythn相关的python教程和python下载,希望大家能够喜欢
0 0
- python 多线程应用
- python多线程应用实例
- python 多线程应用
- python 多线程应用
- python 多线程简单应用
- python Queue的多线程应用
- Python中socket和多线程的应用
- 手把手教您Python多线程应用技巧
- 手把手教您Python多线程应用技巧
- python多线程在渗透测试中的应用
- python--threading 多线程的简单应用
- python多线程在socket编程中的应用(代码范例)
- 条件变量 condition 在 python 多线程编程中的应用
- python多线程中互斥锁Threading.Lock的简单应用
- 基于Python多线程的TCP客户端/服务端应用示例
- 图书排名查询——python多线程编程应用
- python udp的应用 ,多线程实现聊天功能
- 多线程应用
- word2vec 中文训练小试牛刀
- 【数据结构】第6章 树(下)
- 【Linux】Ubuntu系统下VI编辑器方向键输出字母问题
- Python多线程学习
- OpenSessionInViewFilter 的配置及替代方案
- python 多线程应用
- OC之内存管理
- python 多线程就这么简单
- 在做自动化测试之前你需要知道的
- Ubuntu下,Eclipse中Python-interpreter配置MonkeyRunner
- hdu 2516 斐波那契博弈
- python 多线程就这么简单(续)
- 指针与结构体
- OC之封装