URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 5 » [精华] 发现一个可以在 进程 间共享数据的办法.
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
[精华] 发现一个可以在 进程 间共享数据的办法.
 
 
 
 
 
 
 
Subject: [精华] 发现一个可以在 进程 间共享数据的办法.
Author: panhudie    Posted: 2005-10-29 21:51    Length: 379 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]
Subject: [精华] Re: 发现一个可以在 进程 间共享数据的办法.
Author: yjfkdm    Posted: 2005-10-30 13:26    Length: 9 byte(s)
[Original] [Print] [Top]
好东西!!!
[Original] [Print] [Top]
Subject: Re: 发现一个可以在 进程 间共享数据的办法.
Author: gutounan    Posted: 2005-10-30 14:52    Length: 301 byte(s)
[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]
Subject: Re: 发现一个可以在 进程 间共享数据的办法.
Author: panhudie    Posted: 2005-10-30 16:15    Length: 888 byte(s)
[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]
Subject: Re: 发现一个可以在 进程 间共享数据的办法.
Author: passworld    Posted: 2005-10-31 13:14    Length: 71 byte(s)
[Original] [Print] [Top]
0 是 stdin ,标准输入, -1 什么也不是,是错误标志。

----
[Original] [Print] [Top]
Subject: Re: 发现一个可以在 进程 间共享数据的办法.
Author: panhudie    Posted: 2005-10-31 13:31    Length: 1,578 byte(s)
[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]
Subject: Re: 发现一个可以在 进程 间共享数据的办法.
Author: xyb    Posted: 2005-10-31 14:45    Length: 1,020 byte(s)
[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]
« Previous thread
请教 limodou : 关于 Laszlo
Python 编 程
5
Next thread »
通过例子学大家一起学wxPython!欢迎高手支持。
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:11:01, cost 0.041021823883057 ms.