开发者

using mmap in python

can somebody please explain how does 0 influence mmap in python in this case:

mmap.mmap(0 , 256, "some tag")

I thought that I always need to transfer f开发者_C百科ile descriptor rather than 0, so why zero?


From reading CPython 2.7 source code, it seems that on Windows, specifying fileno = 0 has the same effect as specifying fileno = -1, where the latter means "map anonymous memory".

Only -1 is accepted on Unix: on my 64-bit Ubuntu box with Python 2.6.5, mmap.mmap(0, 256) fails with errno=19 (No such device) and mmap.mmap(-1, 256) works fine.

Bottom line: fileno = 0 is a non-portable Windows-only variant of fileno = -1. It may get deprecated (there's even a commented-out warning in the code to that effect).

P.S. The CPython source file in question is Modules/mmapmodule.c.


You can take a look at the following discussion to know more.

In general if first parameter(fileno) is 0 or -1 then your statement says that you allocate 256 bytes of memory, shared across processes and not backed up by a file in the filesystem.

By the way i have tested this code in WinXp with Python 2.5, 2.6, 2.7 and 3.0 and all works fine.


On *nix, file number 0 denotes standard input. So your call would map standard input if it came from a file (you can't mmap a terminal device) and if that file was opened read-write (otherwise you'd have to add access=mmap.ACCESS_READ or similar). In cases where this is indeed intended behavior, I'd suggest writing it as sys.stdin.fileno() instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜