|
|
|
|
| [精华] 发现一个可以在 进程 间共享数据的办法. |
 [精华] 发现一个可以在 进程 间共享数据的办法. - panhudie [ 2005-10-29 21:51 | 379 byte(s)]
 Re: 发现一个可以在 进程 间共享数据的办法. - xyb [ 2005-10-31 14:45 | 1,020 byte(s)]
 [精华] Re: 发现一个可以在 进程 间共享数据的办法. - yjfkdm [ 2005-10-30 13:26 | 9 byte(s)]
 Re: 发现一个可以在 进程 间共享数据的办法. - gutounan [ 2005-10-30 14:52 | 301 byte(s)]
 Re: 发现一个可以在 进程 间共享数据的办法. - panhudie [ 2005-10-30 16:15 | 888 byte(s)]
 Re: 发现一个可以在 进程 间共享数据的办法. - passworld [ 2005-10-31 13:14 | 71 byte(s)]
 Re: 发现一个可以在 进程 间共享数据的办法. - panhudie [ 2005-10-31 13:31 | 1,578 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
# process 1
from mmap import mmap
f = mmap(0, 1024*4, 'name')
f.write('process1
')
# process 2
from mmap import mmap
f = mmap(0, 1024*4, 'name')
f.readline() # --> process1
只能在windows中用, 只要''name''是一样就行了
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
mmap好像不是这样用的吧
windows的不知道
刚才试了一下linux的
需要建一个文件file1
文件的尺寸要大于等于下面的1024
然后这样
fd = open('file1', 'r+')
f = mmap(fd.fileno(), 1024)
f.write('Something here')
read操作类似
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
其实我想mmap的第一个parameter 因该是用 -1(INVALID_HANDLE_VALUE), 但是用-1 要报错,
报错的代码因该是下面这个:
if (fh==(HANDLE)-1) {
PyErr_SetFromErrno(mmap_module_error);
return NULL;}
我又试了一下0, 虽然不知道为什么可以, 但是好像效果是一样的, 都是在OS的page上创建一个filemap, 这样就可以不自己先创建一个文件在map了, 这个临时的share memory因该会在没有任何进程reference的时候自动被OS销毁
具体方法是参考这本书:
Ms Press - Programming Applications For Microsoft Windows, 4Th Edition - Recompiled.chm
(非常棒的一本书)
刚才google了下发现了这个
http://mail.python.org/pipermail/python-list/2003-November/196709.html
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
在windows下 INVALID_HANDLE_VALUE 的值是 -1, 至于为什么用sdtin可以, 效果好像也差不多, 这个可能只有看了windows下CreateFileMapping的代码才知道.
Most Windows functions that return a handle return NULL when they are unsuccessful. CreateFile, however, returns INVALID_HANDLE_ VALUE, which is defined as ((HANDLE) -1).
Here is an interesting problem that has caught unsuspecting programmers by surprise. Can you guess what is wrong with the following code fragment?
HANDLE hFile = CreateFile(...);
HANDLE hMap = CreateFileMapping(hFile, ...);
if (hMap == NULL)
return(GetLastError());
If the call to CreateFile above fails, it returns INVALID_HANDLE_VALUE. However, the unsuspecting programmer who wrote this code didn't test to check whether the file was created successfully. When CreateFileMapping is called, INVALID_HANDLE_ VALUE is passed in the hFile parameter, which causes the system to create a file mapping using storage from the paging file instead of the intended disk file. Any additional code that uses the memory-mapped file will work correctly. However, when the file-mapping object is destroyed, all the data that was written to the file-mapping storage (the paging file) will be destroyed by the system. At this point, the developer sits and scratches his or her head, wondering what went wrong! You must always check CreateFile's return value to see if an error occurred because CreateFile can fail for so many reasons!
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
unix传统进程通讯(IPC)的方式 :文件,命名管道,共享内存(mmap)。windows下面的不是特别清楚,不过mmap这种方式的区别应该不是特别大:
http://docs.python.org/lib/module-mmap.html
For both the Unix and Windows versions of the function, access may be specified as an optional keyword parameter. access accepts one of three values: ACCESS_READ, ACCESS_WRITE, or ACCESS_COPY to specify readonly, write-through or copy-on-write memory respectively. access can be used on both Unix and Windows. If access is not specified, Windows mmap returns a write-through mapping. The initial memory values for all three access types are taken from the specified file. Assignment to an ACCESS_READ memory map raises a TypeError exception. Assignment to an ACCESS_WRITE memory map affects both memory and the underlying file. Assignment to an ACCESS_COPY memory map affects memory but does not update the underlying file.
|
|
|
[Original]
[Print]
[Top]
|
|
|