国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

python相似模塊用例

來源:懂視網 責編:小采 時間:2020-11-27 14:35:35
文檔

python相似模塊用例

python相似模塊用例:一:threading VS Thread 眾所周知,python是支持多線程的,而且是native的線程,其中threading是對Thread模塊做了包裝,可以更加方面的被使用,threading模塊里面主要對一些線程操作對象化了,創建了Thread的類。 使用線程有兩種模式,一種是創建線程
推薦度:
導讀python相似模塊用例:一:threading VS Thread 眾所周知,python是支持多線程的,而且是native的線程,其中threading是對Thread模塊做了包裝,可以更加方面的被使用,threading模塊里面主要對一些線程操作對象化了,創建了Thread的類。 使用線程有兩種模式,一種是創建線程

一:threading VS Thread

眾所周知,python是支持多線程的,而且是native的線程,其中threading是對Thread模塊做了包裝,可以更加方面的被使用,threading模塊里面主要對一些線程操作對象化了,創建了Thread的類。

使用線程有兩種模式,一種是創建線程要執行的函數,把這個函數傳遞進Thread對象里,讓它來執行,一種是直接從Thread繼承,創建一個新的class,把線程執行的代碼放到這個新的類里面,用例如下:

①使用Thread來實現多線程

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import string
import threading 
import time

def threadMain(a):
 global count,mutex
 #獲得線程名
 threadname = threading.currentThread().getName()

 for x in xrange(0,int(a)):
 #獲得鎖
 mutex.acquire()
 count += 1
 #釋放鎖
 mutex.release()
 print threadname,x,count
 time.sleep()

def main(num):
 global count,mutex
 threads = []
 count = 1
 #創建一個鎖
 mutex = threading.Lock()
 #先創建線程對象
 for x in xrange(0,num):
 threads.append(threading.Thread(target = threadMain,args=(10,)))
 for t in threads:
 t.start()
 for t in threads:
 t.join()

if __name__ == "__main__":
 num = 4
 main(num);

②使用threading來實現多線程

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import threading
import time

class Test(threading.Thread):
 def __init__(self,num):
 threading.Thread.__init__(self):
 self._run_num = num

 def run(self):
 global count,mutex
 threadName = threading.currentThread.getName()
 for x in xrange(0,int(self._run_num)):
 mutex.acquire()
 count += 1
 mutex.release()
 print threadName,x,count
 time.sleep(1)

if __name__ == "__main__":
 global count,mutex
 threads = []
 num = 4
 count = 1
 mutex.threading.Lock()
 for x in xrange(o,num):
 threads.append(Test(10))
 #啟動線程
 for t in threads:
 t.start()
 #等待子線程結束
 for t in threads:
 t.join()

二:optparser VS getopt

①使用getopt模塊處理Unix模式的命令行選項

getopt模塊用于抽出命令行選項和參數,也就是sys.argv,命令行選項使得程序的參數更加靈活,支持短選項模式和長選項模式

例:python scriptname.py –f “hello” –directory-prefix=”/home” –t --format ‘a'‘b'

getopt函數的格式:getopt.getopt([命令行參數列表],‘短選項',[長選項列表])

其中短選項名后面的帶冒號(:)表示該選項必須有附加的參數

長選項名后面有等號(=)表示該選項必須有附加的參數

返回options以及args

options是一個參數選項及其value的元組((‘-f','hello'),(‘-t',''),(‘—format',''),(‘—directory-prefix','/home'))

args是除去有用參數外其他的命令行 輸入(‘a',‘b')

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys
import getopt

def Usage():
 print "Usage: %s [-a|-0|-c] [--help|--output] args..."%sys.argv[0]

if __name__ == "__main__":
 try:
 options,args = getopt.getopt(sys.argv[1:],"ao:c",['help',"putput="]):
 print options
 print "
"
 print args

 for option,arg in options:
 if option in ("-h","--help"):
 Usage()
 sys.exit(1)
 elif option in ('-t','--test'):
 print "for test option"
 else:
 print option,arg
 except getopt.GetoptError:
 print "Getopt Error"
 Usage()
 sys.exit(1)

②optparser模塊

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import optparser
def main():
 usage = "Usage: %prog [option] arg1,arg2..."
 parser = OptionParser(usage=usage)
 parser.add_option("-v","--verbose",action="store_true",dest="verbose",default=True,help="make lots of noise [default]")
 parser.add_option("-q","--quiet",action="store_false",dest="verbose",help="be vewwy quiet (I'm hunting wabbits)")
 parser.add_option("-f","--filename",metavar="FILE",help="write output to FILE")
 parser.add_option("-m","--mode",default="intermediate",help="interaction mode: novice, intermediate,or expert [default: %default]")
 (options,args) = parser.parse_args()
 if len(args) != 1:
 parser.error("incorrect number of arguments")
 if options.verbose:
 print "reading %s..." %options.filename 

if __name__ == "__main__":
 main()

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

python相似模塊用例

python相似模塊用例:一:threading VS Thread 眾所周知,python是支持多線程的,而且是native的線程,其中threading是對Thread模塊做了包裝,可以更加方面的被使用,threading模塊里面主要對一些線程操作對象化了,創建了Thread的類。 使用線程有兩種模式,一種是創建線程
推薦度:
標簽: 相同 模塊 相似
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 欧美一区二区三区视频 | 理论片国产 | 亚洲欧美中文日韩综合 | 91久久精品国产亚洲 | 国产成人91一区二区三区 | 国产日韩欧美一区 | www.a级片 | 91精品久久久久久久久久 | 成人午夜精品 | 中文字幕欧美在线 | 欧美资源在线 | 欧美精品国产 | 国产浴室偷窥在线播放 | 国产成人精品影视 | 91免费高清无砖码区 | 亚洲一区中文字幕在线 | 国产产一区二区三区久久毛片国语 | 欧美日韩视频 | 国产不卡视频在线观看 | 一级黄毛片 | 亚洲国产成人久久一区二区三区 | 欧美日本在线观看 | 久久99精品一久久久久久 | 久久精品免费一区二区视 | 欧美日韩国产一区二区三区在线观看 | 九九久久香港经典三级精品 | 国产一区二区三区亚洲欧美 | 欧美日韩中字 | 中文字幕亚洲综合 | 国产免费一区二区三区免费视频 | 国产一区亚洲二区三区毛片 | 国产不卡视频在线播放 | 亚洲欧美日韩中文字幕一区二区三区 | 久久亚洲伊人成综合人影院 | 一级毛片在线全部免费播放 | 亚洲va在线va天堂va四虎 | 精品久久久一二三区 | 精品国产一区二区三区成人 | 精品欧美| 全免费毛片在线播放 | 亚洲精美视频 |