国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python實現多線程的兩種方式

python實現多線程的兩種方式

2020-08-23 13:53趙浮云的blog Python

這篇文章主要為大家詳細介紹了python實現多線程的兩種方式,感興趣的朋友可以參考一下

目前python 提供了幾種多線程實現方式 thread,threading,multithreading ,其中thread模塊比較底層,而threading模塊是對thread做了一些包裝,可以更加方便的被使用。
2.7版本之前python對線程的支持還不夠完善,不能利用多核CPU,但是2.7版本的python中已經考慮改進這點,出現了multithreading  模塊。threading模塊里面主要是對一些線程的操作對象化,創建Thread的class。一般來說,使用線程有兩種模式:

A 創建線程要執行的函數,把這個函數傳遞進Thread對象里,讓它來執行;
B 繼承Thread類,創建一個新的class,將要執行的代碼 寫到run函數里面。

本文介紹兩種實現方法。
第一種 創建函數并且傳入Thread 對象中
t.py 腳本內容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import threading,time
from time import sleep, ctime
def now() :
  return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) )
def test(nloop, nsec):
  print 'start loop', nloop, 'at:', now()
sleep(nsec)
  print 'loop', nloop, 'done at:', now()
def main():
  print 'starting at:',now()
  threadpool=[]
for i in xrange(10):
    th = threading.Thread(target= test,args= (i,2))
    threadpool.append(th)
for th in threadpool:
    th.start()
for th in threadpool :
    threading.Thread.join( th )
  print 'all Done at:', now()
if __name__ == '__main__':
    main()

 thclass.py 腳本內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import threading ,time
from time import sleep, ctime
def now() :
  return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) )
class myThread (threading.Thread) :
"""docstring for myThread"""
   def __init__(self, nloop, nsec) :
     super(myThread, self).__init__()
     self.nloop = nloop
     self.nsec = nsec
   def run(self):
     print 'start loop', self.nloop, 'at:', ctime()
sleep(self.nsec)
     print 'loop', self.nloop, 'done at:', ctime()
def main():
   thpool=[]
   print 'starting at:',now()
for i in xrange(10):
     thpool.append(myThread(i,2))
for th in thpool:
     th.start()
for th in thpool:
     th.join()
   print 'all Done at:', now()
if __name__ == '__main__':
    main()

以上就是本文的全部內容嗎,希望對大家學習python程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久嗨 | 欧美成人高清 | 黄色国产网站 | 色伊人| 黄色一级免费大片 | 无毛网站| 欧美一级黄 | 中文字幕在线视频观看 | 色片在线观看 | 国产毛片久久久久 | 成人3d动漫一区二区三区91 | 少妇一级淫免费放 | 精品第一页 | 久久久久久久久综合 | 搞黄在线观看 | 在线精品国产一区二区三区 | 免费日韩一级片 | 国产99久久 | 日韩综合一区 | 国产精品ssss在线亚洲 | 亚洲精品www久久久久久广东 | 日日夜夜国产 | 午夜视频在线网站 | 亚洲精品久久久久久久久久久 | 国产一区 | 最新国产在线 | 操操网 | 日韩在线一区二区 | 色婷婷一二三 | 日韩中文一区二区三区 | 久久久久久高清 | 极品美女销魂一区二区三区 | 亚洲国产精品自拍 | 久久国产一区 | 久久久久久久久久久久福利 | 国产精品毛片久久久 | 精品国产乱码久久久久久影片 | 日韩国产欧美 | 亚洲一区在线视频 | 一区二区三区在线免费观看 | 欧美日韩国产精品 |