python用多线程可以快几倍_python多线程的几种方法

Python多线程编程中常用方法:

1、join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join([timeout]) timeout:可选参数,线程运行的最长时间

2、isAlive()方法:查看线程是否还在运行

3、getName()方法:获得线程名

4、setDaemon()方法:主线程退出时,需要子线程随主线程退出,则设置子线程的setDaemon()

Python线程同步:

(1)Thread的Lock和RLock实现简单的线程同步:

copycode.gif

importthreading

importtime

classmythread(threading.Thread):

def __init__(self,threadname):

threading.Thread.__init__(self,name=threadname)

defrun(self):

globalx

lock.acquire()

for i in range(3):

x = x+1time.sleep(1)

printx

lock.release()

if __name__ == '__main__':

lock =threading.RLock()

t1 =[]

for i in range(10):

t =mythread(str(i))

t1.append(t)

x =0

for i int1:

i.start()

copycode.gif

(2)使用条件变量保持线程同步:

copycode.gif

#coding=utf-8

importthreading

classProducer(threading.Thread):

def __init__(self,threadname):

threading.Thread.__init__(self,name=threadname)

defrun(self):

globalx

con.acquire()

if x == 10000:

con.wait()

pass

else:

for i in range(10000):

x = x+1con.notify()

printx

con.release()

classConsumer(threading.Thread):

def __init__(self,threadname):

threading.Thread.__init__(self,name=threadname)

defrun(self):

globalx

con.acquire()

if x ==0:

con.wait()

pass

else:

for i in range(10000):

x = x-1con.notify()

printx

con.release()

if __name__ == '__main__':

con =threading.Condition()

x =0

p = Producer('Producer')

c = Consumer('Consumer')

p.start()

c.start()

p.join()

c.join()

print x

copycode.gif

(3)使用队列保持线程同步:

copycode.gif

#coding=utf-8

importthreading

importQueue

importtime

importrandom

classProducer(threading.Thread):

def __init__(self,threadname):

threading.Thread.__init__(self,name=threadname)

defrun(self):

globalqueue

i = random.randint(1,5)

queue.put(i)

print self.getName(),'put %d to queue' %(i)

time.sleep(1)

classConsumer(threading.Thread):

def __init__(self,threadname):

threading.Thread.__init__(self,name=threadname)

defrun(self):

globalqueue

item =queue.get()

print self.getName(),'get %d from queue' %(item)

time.sleep(1)

if __name__ == '__main__':

queue =Queue.Queue()

plist =[]

clist =[]

for i in range(3):

p = Producer('Producer'+str(i))

plist.append(p)

for j in range(3):

c = Consumer('Consumer'+str(j))

clist.append(c)

for pt inplist:

pt.start()

pt.join()

for ct inclist:

ct.start()

ct.join()

copycode.gif

生产者消费者模式的另一种实现:

copycode.gif

#coding=utf-8

importtime

importthreading

importQueue

classConsumer(threading.Thread):

def __init__(self, queue):

threading.Thread.__init__(self)

self._queue =queue

defrun(self):

whileTrue:

#queue.get() blocks the current thread until an item is retrieved.

msg =self._queue.get()

#Checks if the current message is the "quit"

if isinstance(msg, str) and msg == 'quit':

#if so, exists the loop

break

#"Processes" (or in our case, prints) the queue item

print "I'm a thread, and I received %s!!" %msg

#Always be friendly!

print 'Bye byes!'

classProducer(threading.Thread):

def __init__(self, queue):

threading.Thread.__init__(self)

self._queue =queue

defrun(self):

#variable to keep track of when we started

start_time =time.time()

#While under 5 seconds..

while time.time() - start_time < 5:

#"Produce" a piece of work and stick it in the queue for the Consumer to process

self._queue.put('something at %s' %time.time())

#Sleep a bit just to avoid an absurd number of messages

time.sleep(1)

#This the "quit" message of killing a thread.

self._queue.put('quit')

if __name__ == '__main__':

queue =Queue.Queue()

consumer =Consumer(queue)

consumer.start()

producer1 =Producer(queue)

producer1.start()

copycode.gif

使用线程池(Thread pool)+同步队列(Queue)的实现方式:

copycode.gif

#A more realistic thread pool example#coding=utf-8

importtime

importthreading

importQueue

importurllib2

classConsumer(threading.Thread):

def __init__(self, queue):

threading.Thread.__init__(self)

self._queue =queue

defrun(self):

whileTrue:

content =self._queue.get()

if isinstance(content, str) and content == 'quit':

breakresponse =urllib2.urlopen(content)

print 'Bye byes!'

defProducer():

urls =[

'http://www.python.org', 'http://www.yahoo.com'

'http://www.scala.org', 'http://cn.bing.com'

#etc..

]

queue =Queue.Queue()

worker_threads = build_worker_pool(queue, 4)

start_time =time.time()

#Add the urls to process

for url inurls:

queue.put(url)

#Add the 'quit' message

for worker inworker_threads:

queue.put('quit')

for worker inworker_threads:

worker.join()

print 'Done! Time taken: {}'.format(time.time() -start_time)

defbuild_worker_pool(queue, size):

workers =[]

for _ inrange(size):

worker =Consumer(queue)

worker.start()

workers.append(worker)

returnworkers

if __name__ == '__main__':

Producer()

copycode.gif

另一个使用线程池+Map的实现:

copycode.gif

importurllib2

from multiprocessing.dummy importPool as ThreadPool

urls =[

'http://www.python.org',

'http://www.python.org/about/',

'http://www.python.org/doc/',

'http://www.python.org/download/',

'http://www.python.org/community/']

#Make the Pool of workers

pool = ThreadPool(4)

#Open the urls in their own threads#and return the results

results =pool.map(urllib2.urlopen, urls)

#close the pool and wait for the work to finish

pool.close()

pool.join()

copycode.gif

参考: http://blog.jobbole.com/58700

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值