Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन में MMAP फंक्शन से फाइल रीडिंग परफॉरमेंस कैसे सुधारें

परिचय

MMAP (मेमोरी मैपिंग) जब किसी फाइल से मैप किया जाता है, तो डेटा को सामान्य I/O फंक्शन्स के बजाय ऑपरेटिंग सिस्टम की वर्चुअल मेमोरी का उपयोग करके फाइल सिस्टम पर सीधे एक्सेस करता है, जिससे I/O परफॉरमेंस में सुधार होता है क्योंकि इसके लिए प्रत्येक एक्सेस के लिए अलग सिस्टम कॉल या बफ़र्स के बीच डेटा कॉपी करने की आवश्यकता नहीं होती।

वास्तव में, मेमोरी में कुछ भी - उदाहरण के लिए एक SQLite डेटाबेस जब इन-मेमोरी बनाया जाता है, तो ऑन-डिस्क की तुलना में बेहतर परफॉरमेंस देता है।

मेमोरी-मैप्ड फाइलों को म्यूटेबल स्ट्रिंग्स या फाइल-जैसे ऑब्जेक्ट्स के रूप में माना जा सकता है, यह इस बात पर निर्भर करता है कि आप क्या करना चाहते हैं।

MMAP कई मेथड्स सपोर्ट करता है, जैसे close(), flush(), read(), readline(), seek(), tell(), write() और स्लाइस ऑपरेशन्स और यहां तक कि रेगुलर एक्सप्रेशन्स के साथ भी अच्छी तरह काम कर सकता है।

कैसे करें

1. एक टेक्स्ट फाइल मान लें जिसकी सामग्री नीचे दी गई है। आप इसे Google पर सैंपल टेक्स्ट खोजकर प्राप्त कर सकते हैं। इन सामग्रियों को input.txt फाइल में कॉपी करें।

2. mmap() फंक्शन का उपयोग करके मेमोरी-मैप्ड फाइल बनाएं। हम फाइलनेम को या तो फाइल ऑब्जेक्ट के fileno() मेथड से या os.open() से पास कर सकते हैं।

नोट: यूजर mmap() को इनवोक करने से पहले फाइल खोलने और बाद में बंद करने के लिए जिम्मेदार है।

mmap() का दूसरा आर्गुमेंट बाइट्स में एक साइज है जो मैप करने के लिए फाइल के हिस्से को इंगित करता है। अगर मान 0 है, तो पूरी फाइल मैप हो जाती है। एक अतिरिक्त आर्गुमेंट भी है जिसका उपयोग आप कर सकते हैं: ACCESS_READ रीड-ओनली एक्सेस के लिए, ACCESS_WRITE राइट-थ्रू एक्सेस के लिए, और ACCESS_COPY कॉपी-ऑन-राइट एक्सेस के लिए।


import mmap

input_text = """Lorem ipsum dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. No nam eirmod deterruisset, nusquam electram rationibus ad sea, interesset delicatissimi et sit. Purto molestiae cu eum, in per hinc periculis intellegam. Id porro facete cum. No est veritus detraxit facilisis, sit ea clita decore essent. Ut eam labores fuisset menandri, ex sit brute viderer eleifend, altera argumentum vel ex. Duo at zril sensibus, eu vim ullum assentior, quando possit at his. Te nam tempor posidonium scripserit, eam mundi reprimique dissentias ne. Vim te soleat offendit democritum. Nam an diam elaboraret, quaeque dissentias an has. Autem legendos dignissim ad vis, sea ex amet petentium reprehendunt, inermis constituam philosophia ne mel. Esse noster lobortis usu ne. Nec reque postea urbanitas ut, mea in nulla invidunt ocurreret. Ei duo iuvaret numquam. Ferri nemore audire te est, mel et detracto noluisse. Nec eu habeo justo, id pro posse apeirian volutpat. Mea sonet quaestio ne. Atqui quaeque alienum te vim. Graeco aliquip liberavisse pro ut. Te similique reformidans usu, te mundi aliquando ius. Meis scripta minimum quo no, meis prima fabellas eu eam, laoreet delicata forensibus ut vim. Et quo vocibus mediocritatem, atqui summo an eam."""

input_file = 'input.txt'
f = open(input_file, "w+")
f.write(input_text)
f.close()

# फाइल को रीड मोड में खोलें
with open(input_file, 'r') as f:
    with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as m:
        print(f"Output \n*** Output first 5 bytes of the {input_file} is {m.read(5)} ")
        print(f"*** Output Next 10 bytes of the {input_file} is {m.read(10)} ")

आउटपुट:


*** Output first 5 bytes of the input.txt is b'Lorem'
*** Output Next 10 bytes of the input.txt is b' ipsum dol'

हमने फाइल पढ़ी और मेमोरी में मैप की और पहले 5 बाइट्स पढ़ने के लिए .read() का उपयोग किया। तो पहली रीड के बाद फाइल पॉइंटर 10 बाइट्स आगे बढ़ जाता है। अब अगर आप एक और रीड करते हैं, मान लीजिए read(10) बाइट्स, तो यह आपको 6-15 बाइट्स देगा।

3. मेमोरी मैप्ड फाइल को अपडेट करने के लिए सेट अप करें

इसे 'r+' (नहीं 'w') के लिए खोलें मैप करने से पहले।

मैं आपको एक उदाहरण दिखाऊंगा कि लाइन के एक हिस्से को इन-प्लेस कैसे मॉडिफाई करें।


import mmap
import shutil

input_file = 'input.txt'
input_copy = input_file.replace('input', 'input_copy')

# फाइल की कॉपी बनाएं ताकि मूल अन-मॉडिफाइड रहे
shutil.copyfile(input_file, input_copy)

word = b'ipsum'
modified_word = word[::-1]  # रिवर्स

# अपडेट प्राप्त करने के लिए फाइल खोलें
with open(input_copy, 'r+') as f:
    with mmap.mmap(f.fileno(), 0) as m:
        print(f"output \n *** Line before updates \n {m.readline().rstrip()}")
        # रिवाइंड करने के लिए seek का उपयोग करें
        m.seek(0)
        # शब्द ढूंढें और रिवर्स करें
        loc = m.find(word)
        m[loc:loc + len(word)] = modified_word
        m.flush()
        # रिवाइंड करने के लिए seek का उपयोग करें
        m.seek(0)
        print(f" \n *** Line after updates \n {m.readline().rstrip()}")
    f.seek(0)
    print(f" \n *** Final file \n {f.readline().rstrip()}")

आउटपुट:


*** Line before updates b'Lorem ipsum dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. No nam eirmod deterruisset, nusquam electram rationibus ad sea, interesset delicatissimi et sit. Purto molestiae cu eum, in per hinc periculis intellegam.'
*** Line after updates b'Lorem muspi dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. No nam eirmod deterruisset, nusquam electram rationibus ad sea, interesset delicatissimi et sit. Purto molestiae cu eum, in per hinc periculis intellegam.'
*** Final file Lorem muspi dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. No nam eirmod deterruisset, nusquam electram rationibus ad sea, interesset delicatissimi et sit. Purto molestiae cu eum, in per hinc periculis intellegam.

शब्द "ipsum" को पहली लाइन के बीच में मेमोरी में और फाइल में बदल दिया गया है।

4. यदि आप केवल इन-मेमोरी परिवर्तन देखना चाहते हैं और डिस्क पर फाइल अपडेट नहीं करना चाहते हैं, तो ACCESS_COPY का उपयोग करें


with open(input_copy, 'r+') as f:
    with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_COPY) as m:
        print(f"output \n *** Line before updates \n {m.readline().rstrip()}")
        m.seek(0)
        loc = m.find(word)
        m[loc:loc + len(word)] = modified_word
        m.flush()
        m.seek(0)
        print(f" \n *** Line after updates \n {m.readline().rstrip()}")
    f.seek(0)
    print(f" \n *** Final file \n {f.readline().rstrip()}")

आउटपुट: इन-मेमोरी परिवर्तन दिखाई देते हैं लेकिन फाइल डिस्क पर अपरिवर्तित रहती है।

  1. नई फाइल सिस्टम के साथ विंडोज परफॉरमेंस कैसे सुधारें नई फाइल सिस्टम के साथ विंडोज परफॉरमेंस कैसे सुधारें

    अपने विंडोज पीसी को मजबूत बनाना चाहते हैं? आप दो तकनीकों के साथ सॉफ्टवेयर प्रदर्शन और स्टोरेज विश्वसनीयता बढ़ा सकते हैं: रिडंडेंट ऐरे ऑफ इंडिपेंडेंट ड्राइव्स (RAID) और माइक्रोसॉफ्ट का रेजिलिएंट फाइल सिस्टम (ReFS)। एकमात्र आवश्यकता है, कम से कम, दो स्पेयर ड्राइव। क्या आप सोच रहे हैं कि क्या ReFS पुरा

  1. Python में def से परिभाषित फंक्शन को कैसे प्लॉट करें? (Matplotlib) Python में def से परिभाषित फंक्शन को कैसे प्लॉट करें? (Matplotlib)

    Python में def से परिभाषित फंक्शन को प्लॉट करने के लिए, हम निम्नलिखित चरण अपना सकते हैं -फिगर का आकार सेट करें और सबप्लॉट्स के बीच और आसपास पैडिंग एडजस्ट करें।def का उपयोग करके यूजर-डिफाइंड फंक्शन बनाएं, यानी f(x)।NumPy का उपयोग करके x डेटा पॉइंट्स बनाएं।plot() मेथड का उपयोग करके x और f(x) प्लॉट करे

  1. पायथन ट्किंटर में asksaveasfile() फ़ंक्शन पायथन ट्किंटर में asksaveasfile() फ़ंक्शन

    TKinter पायथन में GUI प्रोग्रामिंग के लिए एक मॉड्यूल है। हम एक कैनवास बनाते हैं और उस पर कई गुणों और व्यवहारों के साथ अपने UI घटक रखते हैं। इस लेख में, हम देखेंगे कि asksaveasfile फ़ंक्शन का उपयोग करके पायथन प्रोग्राम के माध्यम से बनाई गई फ़ाइलों को स्थानीय ड्राइव में कैसे सहेजा जाए।हम पहले एक कैनवा