tft每日頭條

 > 生活

 > python入門多線程

python入門多線程

生活 更新时间:2024-10-05 11:56:58

# -*- coding: UTF-8 -*- import thread import time # start_new_thread(function,args, kwargs=None) 生成一個新的線程 # allocate_lock() 分片locktype鎖對象 # exit() 給線程退出指令 # acquire(wait=None) 嘗試獲取鎖的對象 # locked() 如果獲取了鎖對象則返回True, 否則,返回False # release() 釋放鎖 def loop0(): print "start loop 0 at:", time.ctime() time.sleep(4) print "loop 0 done at:", time.ctime() def loop1(): print 'start loop 1 at', time.ctime() time.sleep(2) print "loop 1 done at:", time.ctime() def main(): print "starting at: ", time.ctime() # loop0() # loop1() thread.start_new(loop0, ()) thread.start_new(loop1, ()) time.sleep(6) print "all Done at: ", time.ctime() #不加線程時,需要6-7s,通過線程後,腳本運行隻需要4s if __name__ == '__main__': main()

python入門多線程(python之線程)1

# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*- import thread import time # 使用線程和鎖。 # 通過使用鎖,可以在所有線程全部完成執行後立即退出。 # start_new_thread(function,args, kwargs=None) 生成一個新的線程,使用給定的args和可選的kwargs來執行function # allocate_lock() 分配locktype鎖對象 # exit() 給線程退出指令 # acquire(wait=None) 嘗試獲取鎖的對象 # locked() 如果獲取了鎖對象則返回True, 否則,返回False # release() 釋放鎖 loops = [4, 2] def loop(nloop, nsec, lock): print "start loop 0 at:", time.ctime() time.sleep(nsec) print "loop", nloop, 'done at:', time.ctime() lock.release() def main(): print 'starting at:', time.ctime() locks = [] nloops = range(len(loops)) for i in nloops: #分配鎖,獲取鎖對象 lock = thread.allocate_lock() #取得每一個鎖,相當于将“把鎖鎖上”,鎖上之後,将鎖添加到鎖列表locks中 lock.acquire() locks.append(lock) for i in nloops: #調用函數loop(),并傳遞3個參數: # i, 對應loop(nloop, nsec, lock):的nloop # loops[i], 對應loop(nloop, nsec, lock):的nsec # locks[i]:鎖對象,對應函數lock參數 thread.start_new(loop, (i, loops[i], locks[i])) for i in nloops: while locks[i].locked(): pass print 'all Done at:', time.ctime() if __name__ == '__main__': main() #輸出 # starting at: Thu May 12 23:37:28 2022 # start loop 0 at: start loop 0 at:Thu May 12 23:37:28 2022 # Thu May 12 23:37:28 2022 # loop 1 done at: Thu May 12 23:37:30 2022 # loop 0 done at: Thu May 12 23:37:32 2022 # all Done at: Thu May 12 23:37:32 2022

# -*- coding: UTF-8 -*- import threading import time # threadming模塊 # thread,表示執行一個線程的對象 # lock,鎖對象,和thread模塊中的鎖一樣 # Rlock,可重入鎖對象,使單一線程可以再次獲得已持有的鎖(遞歸鎖) # Condition,條件變量對象,使得一個線程等等另一個線程滿足特定的條件,比如改變狀态或者某一個值 # Even,條件變量的通用版本,任意數量的線程等待某一個事件的發生,在該事件發生後所有線程将被激活 # Semaphore: 為線程間共享的有限資源提供一個計數器,如果沒有可用資源時會被阻塞 # BoundSemaphore: 與Semaphore相似,不過他不允許超過初始值 # Timer: 與Thread相似,不過它要在運行前等待一段時間 # Barrier: 創建一個障礙,必須達到指定數量的線程後才可以繼續 # thread模塊:不支持守護線程,當主線程結束後,所有子線程都終止。 # threading.Thread對象屬性 # name, 線程名 # ident: 線程标識符 # daemon: 布爾标志,表示這個線程是否是守護線程 # threading.Thread對象方法 # __init__(group=None, target=None, name=None, args=(), kwargs={}, verbase=None, daemon=None) # 實例化一個線程對象,需要一個可調用的target,以及其參數args或kwargs,還可以傳遞name或group參數。 # 此外verbose 标志也可以接受 # daemon的值将會設定為thread.daemon屬性/标志 # start(),開始執行線程 # run(),定義線程功能,通常在子類中被應用重寫 # join(timeout=None),直到啟動的線程終止之前一直挂起,除非給出了timeout(秒),否則會一直阻塞 # getName(),返回線程名 # setName(),設定線程名 # isAlive/is_alive(),布爾标志,表示這個線程是否存活 # isDaemon(),如果是守護線程,則返回True,否則返回False # setDaemon(daemon),把線程的守護标志設定為布爾值 daemonic(必須在線程 start()之前調用) loops = [4, 2] def loop(nloop, nsec): print 'start loop', nloop, 'at:', time.ctime() time.sleep(nsec) print 'loop', nloop, 'done at:', time.ctime() def main(): print 'starting at:', time.ctime() threads = [] nloops = range(len(loops)) for i in nloops: t = threading.Thread(target=loop, args=(i, loops[i])) threads.append(t) for i in nloops: threads[i].start() for i in nloops: threads[i].join() print 'all done at:', time.ctime() if __name__ == "__main__": main() #輸出結果 # starting at: Fri May 13 00:22:22 2022 # start loop 0 at: Fri May 13 00:22:22 2022 # start loop 1 at: Fri May 13 00:22:22 2022 # loop 1 done at: Fri May 13 00:22:24 2022 # loop 0 done at: Fri May 13 00:22:26 2022 # all done at: Fri May 13 00:22:26 2022

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved