Hi ! I want to comm. between 2 procs with mmap - in Windows XP. Interesting thing I saw yesterday.
When I start my mtest.py without redirection stdout, the program freezed. When I pushed Enter, is was released. (Just like when I use raw_input in the end of the code... :-)) For this effect I need to start my process from "Start Menu/run" "cmd" "mtest.py". When I start this program from Context, PyScripter, Total Commander, I don't see same effect. If I redirect sys.stdout, I don't see this effect. What is the problem ? I do wrong anything ? Or it is a bug ? Or my notebook do this ? (I have another example, and it is buggy too, but I tested only in my notebook.) Thanks for help: dd
import os, sys from mmap import mmap from time import sleep from cPickle import dumps, loads _CLIENT_PACKET='C' _MASTER_PACKET='M' _SIGNALS=[_CLIENT_PACKET, _MASTER_PACKET] _PACKET_HEAD=1+12 class MappedFileMgr: def __init__(self,FileName,IsMaster,InitialSize=1024): self.FileName=FileName self.InitialSize=InitialSize self.IsMaster=IsMaster self.File=None self.MapObj=None self.BlockSleep=0 def Open(self): IsMaster=self.IsMaster mode=['rb+','wb'][int(IsMaster)] f=self.File=open(self.FileName,mode) if IsMaster: f.write('0'*self.InitialSize) f.close() f=self.File=open(self.FileName,'rb+') mo=self.MapObj=mmap(f.fileno(),0) if IsMaster: mo[0]=_CLIENT_PACKET mo[1:13]='%012d'%0 def Close(self): self.MapObj.close() self.File.close() self.MapObj=None self.File=None def WaitForYou(self): YourSign=_SIGNALS[int(not self.IsMaster)] mo=self.MapObj bs=self.BlockSleep # Wait for another side while 1: if mo[0]==YourSign: break else: if bs: sleep(bs) def Send(self,Obj): IsMaster=self.IsMaster MySign=_SIGNALS[int(IsMaster)] mo=self.MapObj self.WaitForYou() # We can send # Check size data=dumps(Obj,1) dlen=len(data) nlen=dlen+_PACKET_HEAD if mo.size()<nlen: mo.resize(nlen) # Create packet, and send it packet='%012d%s'%(dlen,data) mo.seek(1) mo.write(packet) mo[0]=MySign def Receive(self): IsMaster=self.IsMaster MySign=_SIGNALS[int(IsMaster)] mo=self.MapObj mos=mo.size() if len(mo)<>mos: mo.resize(mos) self.WaitForYou() # We can read the packet # Check size plen=int(mo[1:13]) if plen==0: Obj=None else: data=mo[13:13+plen] Obj=loads(data) mo.seek(1) mo[1:13]='%012d'%0 mo[0]=MySign return Obj if __name__=='__main__': mfn=os.path.join(os.getcwd(),'test.dat') m1=MappedFileMgr(mfn,1,20) m1.Open() m2=MappedFileMgr(mfn,0) m2.Open() m1.Send(range(10)) print m2.Receive() print m1.Receive() m2.Send('AAA') print m1.Receive()
import sys,fmapmgr,subprocess,os,time mfn=os.path.join(os.getcwd(),'test.dat') logfn=sys.argv[0]+'.log' ismaster=len(sys.argv)==1 if not ismaster: logfn=logfn.replace('.log','.clt.log') sys.stdout=open(logfn,'w') print time.time() if ismaster: m1=fmapmgr.MappedFileMgr(mfn,1,20) m1.Open() subprocess.Popen([r'c:\python24\python.exe',os.path.join(os.getcwd(),'mtest.py'),'1']) m1.Send(range(10)) print m1.Receive() else: m2=fmapmgr.MappedFileMgr(mfn,0) m2.Open() print m2.Receive() m2.Send('A')
-- http://mail.python.org/mailman/listinfo/python-list