三、《Python Programming on Win32》關(guān)于Win32file摘要及思考

Win32file Module概述

Native File Manipulation定血。
There are times when the standard Python file objects can't meet your requirements, and you need to use the Windows API to manipulate files.
當(dāng)通用的文件操作方法(就是python里面的open毅弧、read睦番、write)無法滿足特殊需求(安全性啊啥的~)時(shí)欢际,這個(gè)時(shí)候windows操作文件的API就是時(shí)候發(fā)揮效果了母市!

This can happen in a number of situations, such as:
? You need to read or write data to or from a Windows pipe.
你需要從一個(gè)windows管道讀出或者寫入數(shù)據(jù)時(shí)。
? You need to set custom Windows security on a file you are creating.
需要設(shè)定特殊文件的安全性時(shí)损趋。
? You need to perform advanced techniques for performance reasons, such as "Overlapped" operations or using completion ports.
Completion ports利用一些線程患久,幫助平衡由I/O請(qǐng)求所引起的負(fù)載
OVERLAPPED I/O(異步I/O)是一個(gè)包含了用于異步輸入輸出的信息的結(jié)構(gòu)體
總之:就是你要提高文件操作性能的時(shí)候用這些。

win32file.WriteFile() 
returns the error code from the operation. 
This is either zero or win32error.ERROR_IO_PENDING if overlapped I/O is used. 
All other error codes are converted to a Python exception.
The win32file.CreateFile() function opens or creates standard files, 
returning a handle to the file. 
Standard files come in many flavors, including synchronous files 
(where read or write operations don't return until the 
operation has completed); asynchronous (or overlapped I/O) files, 
where read and write operations return immediately; 
and temporary files that are automatically deleted when the handle is closed. 
Files may also be opened requesting that
 Windows not cache any file operations, that no buffering is performed, etc.

This function returns a PyHANDLE object. 
PyHANDLEs are simply objects that wrap standard Win32 HANDLEs. 
When a PyHANDLE object goes out of scope, it's automatically closed;
 thus, it's generally not necessary to close
 these HANDLEs as it is necessary when using these from C or C++.
Overlapped I/O
Windows provides a number of techniques for high-performance file I/O. 
The most common is overlapped I/O. Using overlapped I/O, 
the win32file.ReadFile() and win32file.WriteFile() operations are asynchronous 
and return before the actual I/O operation has completed. 
When the I/O operation finally completes, a Windows event is signaled.

Overlapped I/O does have some requirements normal I/O operations don't:
? The operating system doesn't automatically advance the file pointer.
 When not using overlapped I/O, a ReadFile or WriteFile operation automatically
 advances the file pointer, so the next operation automatically reads the subsequent
 data in the file. When usingoverlapped I/O, you must manage the location
 in the file manually.
? The standard technique of returning a Python string object from
 win32file. ReadFile() doesn't work. Because the I/O operation has not 
completed when the call returns, a Python string can't be used.
As you can imagine, the code for performing overlapped I/O is more complex than 
when performing synchronous I/O. Chapter 18, WindowsNT Services, 
contains some sample code that uses basic 
overlapped I/O on a Windows-named pipe.

Win32file Module解析

The win32file module contains functions that interface to the File and other I/O-related Win32 API functions.

CreateFile(FileName,········)
Opens or creates a file or a number of other objects and returns a handle that can access the object.
FileName
The name of the file, pipe, or other resource to open.
Result
A PyHANDLE object to the file.


ReadFile()
Reads data from an open file.
errCode, data = ReadFile(FileHandle, Size, Overlapped)
FileHandle
The file handle identifying the file to read from. This handle typically is obtained from win32file.CreateFile().
Size or ReadBuffer
If Size is specified as a Python integer, it's the number of bytes to read from the file. When using overlapped I/O, a ReadBuffer shouldbe used, indicating where the ReadFile operation should place the data.
這句話的意思是:將如你放的是一個(gè)整型數(shù)據(jù),那么readfile()啊蒋失,你得從這個(gè)文件里讀這么多的數(shù)據(jù)回來返帕,但是一旦是異步文件讀取的話,你必須指定一個(gè)buffer高镐,你想案刃:

  • 正常的同步文件讀取流程是這樣的畸冲,你給我把數(shù)據(jù)挖回來嫉髓,我等著,然后底層I/O操作就吭哧吭哧把數(shù)據(jù)給挖回來了(同時(shí)你一直在礦井口即FileHandle這等著)邑闲,然后把數(shù)據(jù)放到一個(gè)叫做data的“籮筐”內(nèi)~交接完畢算行!
  • 但是異步讀取是這么一個(gè)流程:一天一個(gè)商人找到一個(gè)包工頭(應(yīng)用程序),說:啊苫耸,小劉爸菪稀(readfile())啊,你給我去FileHandle這個(gè)礦井挖一籮筐礦回來褪子!接收到指令后量淌,小劉(readfile())就去找到挖礦工鐵柱(系統(tǒng)底層驅(qū)動(dòng)),說:鐵柱啊嫌褪,你給我去FileHandle這個(gè)礦井挖一筐礦上來呀枢,然后就去干其他事了(走的時(shí)候給了鐵柱一部手機(jī)(overlapped),說“整完了給我電話笼痛,我來收”)裙秋,但是這其中有個(gè)問題啊,鐵柱整好的礦往哪放坝б痢摘刑?同步模式下,鐵柱冒出礦井就可以看到劉工頭在井口等著刻坊,直接往框內(nèi)一放枷恕,就交接完事了,但是這個(gè)不一樣啊谭胚,鐵柱只有在出礦井后才有信號(hào)活尊,這個(gè)時(shí)候打電話給劉工頭,然后就是干等著漏益,指不定工頭啥時(shí)候來呢蛹锰!說不定等的這段時(shí)間都可以再挖一卡車礦了呢!绰疤!說不定等著等著鐵柱都快餓掛了铜犬!而且可以肯定的是這個(gè)時(shí)間內(nèi)其他包工頭不能給鐵柱派活了!!總之就是浪費(fèi)了大量的資源癣猾;因此我們就想啊敛劝,要是我們?cè)诮需F柱下去挖礦的時(shí)候順便告訴它你挖完后直接放到我們指定的地點(diǎn),然后通知我們纷宇,我們就會(huì)來取的(取到data這個(gè)“籮筐”內(nèi))夸盟,放到指定地點(diǎn)后你想干嘛干嘛去,大大提高你的效率像捶!畢竟整個(gè)礦場就靠著(基于)你們這群底層的干活人員來運(yùn)轉(zhuǎn)呢上陕! 科科~

感悟:這其實(shí)就是一個(gè)現(xiàn)代商業(yè)社會(huì)微劇場的縮影,在實(shí)際場景中人們總是很容易的想到優(yōu)化的方式拓春,并且留心生活的極客們?cè)缇桶焉顚懭肓舜a释簿,優(yōu)化了代碼!因此我們平時(shí)留心注意生活硼莽,學(xué)習(xí)生活中的智慧庶溶、優(yōu)化思維然后再拓展發(fā)散到各行各業(yè)各方個(gè)面,余以為這是一種可行的策略懂鸵。

Overlapped
An OVERLAPPED object or None if overlapped I/O isn't being performed. This parameter can be omitted, in which case None is used.
Result
errCode:Either 0, or ERROR_IO_PENDING.
Data:The data read from the file.
這里的data就是上面描述的“籮筐”偏螺。異常代碼這里是兩種要么0要么就是異步I/O掛起提示,0表示數(shù)據(jù)獲取成功匆光,I/O掛起是異步文件讀取的時(shí)候套像,提前返回時(shí)的返回碼。

這里的中層哲學(xué)是:不要想管太多(比如殴穴,我會(huì)很“貪心”地想返回值可以返回異常狀態(tài)凉夯,也許是有的,因?yàn)楫吘怪幻枋隽?是成功采幌,其他字段并沒有描述劲够,這部分得看源碼),這只care成功休傍,其他的除了異步態(tài)下的 ERROR_IO_PENDING就都是異常了征绎,你開發(fā)商只要知道0代表成功了,其他的(除去ERROR_IO_PENDING)都是異常就好了磨取,具體的人柿,你精力旺盛想知道是什么異常也可以去查的,反正我作為一個(gè)包工頭對(duì)上(開發(fā)商)是提供高效率的接口高精度的搜查(可選)忙厌,這想必就是嚴(yán)謹(jǐn)?shù)?strong>做事態(tài)度吧Y灬!逢净!對(duì)下(鐵柱)哥放,合理安排策略使得底層利益最大化歼指,實(shí)現(xiàn)共贏


WriteFile()
Writes data to an open file.
errCode = WriteFile(FileHandle, Data, Overlapped)
FileHandle
The file handle identifying the file to read from. This handle typically is obtained from win32file.CreateFile().
Data
The data to write to the file, as a Python string.

Overlapped
An OVERLAPPED object or None if overlapped I/O isn't being performed. This parameter can be omitted, in which case None is used.
Result
errCode: Either 0 or ERROR_IO_PENDING.


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末甥雕,一起剝皮案震驚了整個(gè)濱河市踩身,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌社露,老刑警劉巖挟阻,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異峭弟,居然都是意外死亡附鸽,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門孟害,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拒炎,“玉大人挪拟,你說我怎么就攤上這事挨务。” “怎么了玉组?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵谎柄,是天一觀的道長。 經(jīng)常有香客問我惯雳,道長朝巫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任石景,我火速辦了婚禮劈猿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘潮孽。我一直安慰自己揪荣,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布往史。 她就那樣靜靜地躺著仗颈,像睡著了一般。 火紅的嫁衣襯著肌膚如雪椎例。 梳的紋絲不亂的頭發(fā)上挨决,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音订歪,去河邊找鬼掉瞳。 笑死,一個(gè)胖子當(dāng)著我的面吹牛掌实,可吹牛的內(nèi)容都是我干的某宪。 我是一名探鬼主播福压,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼或舞!你這毒婦竟也來了荆姆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤映凳,失蹤者是張志新(化名)和其女友劉穎胆筒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诈豌,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡仆救,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了矫渔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片彤蔽。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖庙洼,靈堂內(nèi)的尸體忽然破棺而出顿痪,到底是詐尸還是另有隱情,我是刑警寧澤油够,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布蚁袭,位于F島的核電站,受9級(jí)特大地震影響石咬,放射性物質(zhì)發(fā)生泄漏揩悄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一鬼悠、第九天 我趴在偏房一處隱蔽的房頂上張望删性。 院中可真熱鬧,春花似錦焕窝、人聲如沸蹬挺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汗侵。三九已至,卻和暖如春群发,著一層夾襖步出監(jiān)牢的瞬間晰韵,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國打工熟妓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留雪猪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓起愈,卻偏偏與公主長得像只恨,于是被迫代替她去往敵國和親译仗。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容