當前位置:首頁 » 編程語言 » pythonsetdaemon

pythonsetdaemon

發布時間: 2022-05-15 08:30:29

1. python避免死鎖方法實例分析

python避免死鎖方法實例分析
本文實例講述了python避免死鎖方法。分享給大家供大家參考。具體分析如下:
當兩個或者更多的線程在等待資源的時候就會產生死鎖,兩個線程相互等待。
在本文實例中 thread1 等待thread2釋放block , thread2等待thtead1釋放ablock,
避免死鎖的原則:
1. 一定要以一個固定的順序來取得鎖,這個列子中,意味著首先要取得alock, 然後再去block
2. 一定要按照與取得鎖相反的順序釋放鎖,這里,應該先釋放block,然後是alock
import threading ,time
a = 5
alock = threading.Lock()
b = 5
block = threading.Lock()
def thread1calc():
print "thread1 acquiring lock a"
alock.acquire()
time.sleep(5)
print "thread1 acquiring lock b"
block.acquire()
a+=5
b+=5
print "thread1 releasing both locks"
block.release()
alock.release()
def thread2calc():
print "thread2 acquiring lock b"
block.acquire()
time.sleep(5)
print "thread2 acquiring lock a"
alock.acquire()
time.sleep(5)
a+=10
b+=10
print "thread2 releasing both locks"
block.release()
alock.release()
t = threading.Thread(target = thread1calc)
t.setDaemon(1)
t.start()
t = threading.Thread(target = thread2calc)
t.setDaemon(2)
t.start()
while 1:
time.sleep(300)

輸出:
thread1 acquiring lock a
thread2 acquiring lock b
thread1 acquiring lock b
thread2 acquiring lock a
希望本文所述對大家的Python程序設計有所幫助。

2. python的多線程使用setDaemon有什麼意義

使用setDaemon()和守護線程這方面知識有關, 比如在啟動線程前設置thread.setDaemon(True),就是設置該線程為守護線程,
表示該線程是不重要的,進程退出時不需要等待這個線程執行完成。
這樣做的意義在於:避免子線程無限死循環,導致退不出程序,也就是避免樓上說的孤兒進程。

thread.setDaemon()設置為True, 則設為true的話 則主線程執行完畢後會將子線程回收掉,
設置為false,主進程執行結束時不會回收子線程

3. python的多線程使用setDaemon有什麼意義

setDaemon()方法。主線程A中,創建了子線程B,並且在主線程A中調用了B.setDaemon(True),把主線程A設置為守護線程,這時候,要是主線程A執行結束了,就不管子線程B是否完成,一並和主線程A退出.這就是setDaemon方法的含義,這基本和join是相反的。此外,還有個要特別注意的:必須在start() 方法調用之前設置,如果不設置為守護線程,程序會被無限掛起。

4. Python多線程的一些問題

python提供了兩個模塊來實現多線程thread 和threading ,thread 有一些缺點,在threading 得到了彌補,為了不浪費你和時間,所以我們直接學習threading 就可以了。
繼續對上面的例子進行改造,引入threadring來同時播放音樂和視頻:
#coding=utf-8import threadingfrom time import ctime,sleepdef music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime())
sleep(1)def move(func): for i in range(2): print "I was at the %s! %s" %(func,ctime())
sleep(5)

threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡達',))
threads.append(t2)if __name__ == '__main__': for t in threads:
t.setDaemon(True)
t.start() print "all over %s" %ctime()

import threading
首先導入threading 模塊,這是使用多線程的前提。

threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)
創建了threads數組,創建線程t1,使用threading.Thread()方法,在這個方法中調用music方法target=music,args方法對music進行傳參。 把創建好的線程t1裝到threads數組中。
接著以同樣的方式創建線程t2,並把t2也裝到threads數組。

for t in threads:
t.setDaemon(True)
t.start()
最後通過for循環遍歷數組。(數組被裝載了t1和t2兩個線程)

setDaemon()
setDaemon(True)將線程聲明為守護線程,必須在start() 方法調用之前設置,如果不設置為守護線程程序會被無限掛起。子線程啟動後,父線程也繼續執行下去,當父線程執行完最後一條語句print "all over %s" %ctime()後,沒有等待子線程,直接就退出了,同時子線程也一同結束。

start()
開始線程活動。

運行結果:
>>> ========================= RESTART ================================
>>> I was listening to 愛情買賣. Thu Apr 17 12:51:45 2014 I was at the 阿凡達! Thu Apr 17 12:51:45 2014 all over Thu Apr 17 12:51:45 2014

從執行結果來看,子線程(muisc 、move )和主線程(print "all over %s" %ctime())都是同一時間啟動,但由於主線程執行完結束,所以導致子線程也終止。

繼續調整程序:
...if __name__ == '__main__': for t in threads:
t.setDaemon(True)
t.start()

t.join() print "all over %s" %ctime()

我們只對上面的程序加了個join()方法,用於等待線程終止。join()的作用是,在子線程完成運行之前,這個子線程的父線程將一直被阻塞。
注意: join()方法的位置是在for循環外的,也就是說必須等待for循環里的兩個進程都結束後,才去執行主進程。
運行結果:
>>> ========================= RESTART ================================
>>> I was listening to 愛情買賣. Thu Apr 17 13:04:11 2014 I was at the 阿凡達! Thu Apr 17 13:04:11 2014I was listening to 愛情買賣. Thu Apr 17 13:04:12 2014I was at the 阿凡達! Thu Apr 17 13:04:16 2014all over Thu Apr 17 13:04:21 2014

從執行結果可看到,music 和move 是同時啟動的。
開始時間4分11秒,直到調用主進程為4分22秒,總耗時為10秒。從單線程時減少了2秒,我們可以把music的sleep()的時間調整為4秒。
...def music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime())
sleep(4)
...

子線程啟動11分27秒,主線程運行11分37秒。
雖然music每首歌曲從1秒延長到了4 ,但通多程線的方式運行腳本,總的時間沒變化。

5. Python中threading的join和setDaemon的區別及用法

python中得thread的一些機制和C/C++不同:在C/C++中,主線程結束後,其子線程會默認被主線程kill掉。而在python中,主線程結束後,會默認等待子線程結束後,主線程才退出。
python對於thread的管理中有兩個函數:join和setDaemon
join:如在一個線程B中調用threada.join(),則threada結束後,線程B才會接著threada.join()往後運行。
setDaemon:主線程A啟動了子線程B,調用b.setDaemaon(True),則主線程結束時,會把子線程B也殺死,與C/C++中得默認效果是一樣的。
#! /usr/bin/env python
import threading
import time
class myThread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name=threadname)
self.st = 2
def run(self):
time.sleep(self.st)
print self.getName()
def setSt(self, t):
self.st = t
def fun1():
t1.start()
print "fun1 done"
def fun2():
t2.start()
print "fun2 done"
t1=myThread("t1")
t2=myThread("t2")
t2.setSt(10);
# t2.setDaemon(True)
fun1()
fun2()
print "now u will see me"

6. python的多線程使用setDaemon有什麼意義

因為python的全局解釋器鎖的機制, 導致python的多線程並不是真正的多線程, 效率上不僅不會比單線程快,反而可能更慢, 所以說是雞肋,要求速度好話,可以用多進程來實現

7. python如何實現線程池

#這個類是線程類,用來在主程序中調用生成一個線程。其實線程池就是線程的集合地,
#能夠解決有效統一的管理線程,基本就達到了線程池的目的;
#這一段代碼是我的爬蟲程序中的一部分,希望對你有用。
classSpider(Thread):
def__init__(self,todo_list):
super().__init__()
self.setDaemon(True)
self.todo_list=todo_list
self.stat=IDLE

defis_idle(self):
returnself.stat==IDLE

defrun(self):
whileTrue:
url=self.todo_list.get()

#開始線程工作


#這個函數就是主函數了,
defmain(max_threads):
########這里和上一個函數就是核心代碼了。
#創建N個線程,並啟動
print('Spawnspiders')
spiders=[Spider(todo_list)foriinrange(max_threads)]
forspdinspiders:
spd.start()


#python主運行代碼:
if__name__=='__main__':
main(max_threads)

只能給你這么多解釋了,如果想弄懂,還是要去看看基礎知識的。

另外可以查一下有沒有封裝好的三方庫。

8. python的多線程使用setDaemon有什麼意義

使用setDaemon()和守護線程這方面知識有關, 比如在啟動線程前設置thread.setDaemon(True),就是設置該線程為守護線程,
表示該線程是不重要的,進程退出時不需要等待這個線程執行完成。
這樣做的意義在於:避免子線程無限死循環,導致退不出程序,也就是避免樓上說的孤兒進程。

thread.setDaemon()設置為True, 則設為true的話 則主線程執行完畢後會將子線程回收掉,
設置為false,主進程執行結束時不會回收子線程

9. python的多線程使用setDaemon有什麼意義

setDaemon 是把主線程變成守護線程。
類似linux中的守護進程。一般是用來當做某種服務的。
如果這服務現在要停止了,那麼是不應該等待其子線程的。
舉個例子:
比如你做了一個http server,往往可以利用父線程分配線程池啟動一個線程給client響應其請求。
如果你這個時候你用的join起動一個線程,那麼在它結束以前會一直阻塞住父線程,下面來的request就無法得到響應了,這個時候就需要讓這個線程父線程變成他的守護線程,也就是setDaemon,這樣它的執行就不會阻止父線程了。
另外,當你要停止這個server的時候,肯定是希望其他線程跟著一起銷毀的,意義就在這里。

10. 用python ,怎麼實現無限循環(非死循環)

用多線程並行處理while,需要 print x的話,用列隊獲取。

考慮多線程,開一個線程來無限累加。

import threading##多線程
def a:
while True:
print("hello")
def b:
print("xxxx")

threads=[]

擴展資

Python 是一門有條理的和強大的面向對象的程序設計語言,類似於Perl, Ruby, Scheme, Java。自從20世紀90年代初Python語言誕生至今,它已被逐漸廣泛應用於系統管理任務的處理和Web編程。Python在設計上堅持了清晰劃一的風格,這使得Python成為一門易讀、易維護,並且被大量用戶所歡迎的、用途廣泛的語言。

熱點內容
奧維地圖伺服器地址怎麼填 發布:2024-04-25 12:40:04 瀏覽:964
低配置游戲玩哪個平台 發布:2024-04-25 12:35:04 瀏覽:558
glinux下載 發布:2024-04-25 12:30:09 瀏覽:83
安卓手機可以用的谷歌叫什麼 發布:2024-04-25 12:05:57 瀏覽:942
linux改變用戶所屬組 發布:2024-04-25 11:50:33 瀏覽:469
rsa加密演算法java代碼 發布:2024-04-25 11:40:07 瀏覽:883
如何改變拉桿箱上的初始密碼 發布:2024-04-25 11:17:23 瀏覽:799
內網掛代理虛擬機如何配置網卡 發布:2024-04-25 11:15:06 瀏覽:687
明日之後緩存怎麼清理 發布:2024-04-25 11:14:56 瀏覽:205
華為mate30怎麼退回安卓版 發布:2024-04-25 11:08:49 瀏覽:898