中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

濟(jì)南網(wǎng)站建設(shè) 薦搜點(diǎn)網(wǎng)絡(luò)廣告聯(lián)盟接單賺錢平臺(tái)

濟(jì)南網(wǎng)站建設(shè) 薦搜點(diǎn)網(wǎng)絡(luò),廣告聯(lián)盟接單賺錢平臺(tái),wordpress 頭像地址,.net如何做直播網(wǎng)站Python多線程編程中常用方法: 1、join()方法:如果一個(gè)線程或者在函數(shù)執(zhí)行的過程中調(diào)用另一個(gè)線程,并且希望待其完成操作后才能執(zhí)行,那么在調(diào)用線程的時(shí)就可以使用被調(diào)線程的join方法join([timeout]) timeout:可選參數(shù)…

Python多線程編程中常用方法:

1、join()方法:如果一個(gè)線程或者在函數(shù)執(zhí)行的過程中調(diào)用另一個(gè)線程,并且希望待其完成操作后才能執(zhí)行,那么在調(diào)用線程的時(shí)就可以使用被調(diào)線程的join方法join([timeout]) timeout:可選參數(shù),線程運(yùn)行的最長時(shí)間

2、isAlive()方法:查看線程是否還在運(yùn)行

3、getName()方法:獲得線程名

4、setDaemon()方法:主線程退出時(shí),需要子線程隨主線程退出,則設(shè)置子線程的setDaemon()

Python線程同步:

(1)Thread的Lock和RLock實(shí)現(xiàn)簡單的線程同步:

import threading
import time
class mythread(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)def run(self):global xlock.acquire()for i in range(3):x = x+1time.sleep(1)print xlock.release()if __name__ == '__main__':lock = threading.RLock()t1 = []for i in range(10):t = mythread(str(i))t1.append(t)x = 0for i in t1:i.start()

(2)使用條件變量保持線程同步:

# coding=utf-8
import threadingclass Producer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)def run(self):global xcon.acquire()if x == 10000:con.wait() passelse:for i in range(10000):x = x+1con.notify()print xcon.release()class Consumer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)def run(self):global xcon.acquire()if x == 0:con.wait()passelse:for i in range(10000):x = x-1con.notify()print xcon.release()if __name__ == '__main__':con = threading.Condition()x = 0p = Producer('Producer')c = Consumer('Consumer')p.start()c.start()p.join()c.join()print x

(3)使用隊(duì)列保持線程同步:

# coding=utf-8
import threading
import Queue
import time
import randomclass Producer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)def run(self):global     queuei = random.randint(1,5)queue.put(i)print self.getName(),' put %d to queue' %(i)time.sleep(1)class Consumer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)def run(self):global     queueitem = 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 in plist:pt.start()pt.join()for ct in clist:ct.start()ct.join()

生產(chǎn)者消費(fèi)者模式的另一種實(shí)現(xiàn):

# coding=utf-8
import time
import threading
import Queueclass Consumer(threading.Thread):def __init__(self, queue):threading.Thread.__init__(self)self._queue = queuedef run(self):while True:# 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 loopbreak# "Processes" (or in our case, prints) the queue itemprint "I'm a thread, and I received %s!!" % msg# Always be friendly!print 'Bye byes!'class Producer(threading.Thread):def __init__(self, queue):threading.Thread.__init__(self)self._queue = queuedef run(self):# variable to keep track of when we startedstart_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 processself._queue.put('something at %s' % time.time())# Sleep a bit just to avoid an absurd number of messagestime.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()

使用線程池(Thread pool)+同步隊(duì)列(Queue)的實(shí)現(xiàn)方式:

# A more realistic thread pool example
# coding=utf-8
import time 
import threading 
import Queue 
import urllib2 class Consumer(threading.Thread): def __init__(self, queue):threading.Thread.__init__(self)self._queue = queue def run(self):while True: content = self._queue.get() if isinstance(content, str) and content == 'quit':breakresponse = urllib2.urlopen(content)print 'Bye byes!'def Producer():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 processfor url in urls: queue.put(url)  # Add the 'quit' messagefor worker in worker_threads:queue.put('quit')for worker in worker_threads:worker.join()print 'Done! Time taken: {}'.format(time.time() - start_time)def build_worker_pool(queue, size):workers = []for _ in range(size):worker = Consumer(queue)worker.start() workers.append(worker)return workersif __name__ == '__main__':Producer()

另一個(gè)使用線程池+Map的實(shí)現(xiàn):

import urllib2 
from multiprocessing.dummy import Pool 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()

http://www.risenshineclean.com/news/53699.html

相關(guān)文章:

  • wordpress頁面屬性模板怎么添加泰安網(wǎng)站推廣優(yōu)化
  • 上海網(wǎng)站建設(shè)技術(shù)托管軟文臺(tái)
  • 政府網(wǎng)站開發(fā)周期自己做一個(gè)網(wǎng)站
  • 網(wǎng)站運(yùn)營流程18款免費(fèi)軟件app下載
  • 杭州網(wǎng)站設(shè)計(jì)詢問藍(lán)韻網(wǎng)絡(luò)跟我學(xué)seo
  • idea網(wǎng)站開發(fā)上海網(wǎng)站建設(shè)開發(fā)
  • 做it的在哪個(gè)網(wǎng)站找工作百度注冊(cè)公司網(wǎng)站
  • android做網(wǎng)站網(wǎng)站怎么做
  • 做網(wǎng)站重慶今天的病毒感染情況
  • 域名設(shè)計(jì)與分析沈陽seo排名優(yōu)化推廣
  • 設(shè)計(jì)需要看的網(wǎng)站有哪些seo英文怎么讀
  • 自己做的網(wǎng)站如何被百度檢索優(yōu)秀的軟文廣告案例
  • 網(wǎng)站建設(shè)優(yōu)化服務(wù)好么搜狗站長平臺(tái)驗(yàn)證網(wǎng)站
  • 太原網(wǎng)站制作電話百度搜索排行
  • 全球最好的設(shè)計(jì)網(wǎng)站怎么找拉新推廣平臺(tái)
  • 網(wǎng)站建設(shè)公司公司好免費(fèi)b2b推廣網(wǎng)站大全
  • 網(wǎng)站建設(shè)軟件下載西安seo服務(wù)公司
  • 網(wǎng)站的字體做多大合適如何推廣自己的產(chǎn)品
  • 建站公司maxsem競價(jià)托管多少錢
  • 網(wǎng)站開發(fā)人月薪網(wǎng)絡(luò)營銷方法有哪幾種
  • 做現(xiàn)貨IC電子網(wǎng)站的惠州seo計(jì)費(fèi)管理
  • 做網(wǎng)站有地域限制嗎專業(yè)seo站長工具
  • 二級(jí)域名子域名大全領(lǐng)碩網(wǎng)站seo優(yōu)化
  • 淘寶網(wǎng)作圖做網(wǎng)站網(wǎng)站托管代運(yùn)營
  • wordpress買域名百度搜索排名優(yōu)化哪家好
  • 如何注銷網(wǎng)站備案號(hào)數(shù)據(jù)分析培訓(xùn)班
  • 網(wǎng)站的運(yùn)營管理方案長沙seo結(jié)算
  • seo如何根據(jù)網(wǎng)站數(shù)據(jù)做報(bào)表怎么找專業(yè)的營銷團(tuán)隊(duì)
  • 如何做轉(zhuǎn)運(yùn)網(wǎng)站引流推廣犯法嗎
  • wordpress添加版權(quán)信息如何進(jìn)行網(wǎng)站性能優(yōu)化