English 中文(简体)
如何从一毫米应用的档案中读线?
原标题:How to read lines from a mmapped file?

Is seems that the mmap interface only supports readline(). If I try to iterate over the object I get character instead of complete lines.

什么是“慢慢慢”方法可以按线读出一粒的立案线?

import sys
import mmap
import os


if (len(sys.argv) > 1):
  STAT_FILE=sys.argv[1]
  print STAT_FILE
else:
  print "Need to know <statistics file name path>"
  sys.exit(1)


with open(STAT_FILE, "r") as f:
  map = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
  for line in map:
    print line # RETURNS single characters instead of whole line
最佳回答

简化<条码>mmap 是

with open(STAT_FILE, "r+b") as f:
    map_file = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
    for line in iter(map_file.readline, b""):
        # whatever

请注意,在A/63/13中,iter(>>的发送inel para amount of by,而在A/63/12中,它需要成为<<>>/code>(即>>>>,而不是b>>

问题回答

我修改了你的榜样:

with open(STAT_FILE, "r+b") as f:
        m=mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
        while True:
                line=m.readline()
                if line ==   : break
                print line.rstrip()

<>Suggestions:

希望这一帮助。

Edit: I did some timing tests on Linux because the comment made me curious. Here is a comparison of timings made on 5 sequential runs on a 137MB text file.

普通档案查阅:

real    2.410 2.414 2.428 2.478 2.490
sys     0.052 0.052 0.064 0.080 0.152
user    2.232 2.276 2.292 2.304 2.320

档案查阅:

real    1.885 1.899 1.925 1.940 1.954
sys     0.088 0.108 0.108 0.116 0.120
user    1.696 1.732 1.736 1.744 1.752

这些时间安排不包括<条码>说明(我除外)。 在这些数字之后,我可以说,用于查阅档案的记忆速度非常快。

<<>python -m cProfile test.py 我得出以下结果:

5432833    2.273    0.000    2.273    0.000 {method  readline  of  file  objects}
5432833    1.451    0.000    1.451    0.000 {method  readline  of  mmap.mmap  objects}

如果Im没有错,那么mmap就会更快。

此外,看来<代码>而不是len(line)的性能低于line =,至少是我如何解释简介员的产出。

下面是简明扼要的:

with open(STAT_FILE, "r") as f:
    m = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
    while True:
        line = m.readline()  
        if line == "": break
        print line
    m.close()

Note that line retains the newline, so you might like to remove it. It is also the reason why if line == "" does the right thing (an empty line is returned as " ").

The reason the original iteration works the way it does is that mmap tries to look like both a file and a string. It looks like a string for the purposes of iteration.

我不知道它为什么可以提供(或选择不提供)<条形码>(<>>/条码>。

视窗上的灰色2.732倍以上是半衰期的快速:

在27MB,509k条线文字档案(我的传承功能并不令人感兴趣,大部分只是读线(非常快):

with open(someFile,"r") as f:
    if usemmap:
        m=mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
    else:
        m=f
        e.parse(m)

With MMAP:

read in 0.308000087738

没有微额信贷计划:

read in 0.680999994278

如果你误用<条码>mmap(>):

with open( /content/drive/MyDrive...... , "r+b") as f:
    # map_file = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) as mmap not recogn. import something
    for line in iter(f.readline, b""):
      print(line)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签