Duplex communication with pipes - is possible ?

Dara Durum durumdara at gmail.com
Mon Jun 26 03:28:52 EDT 2006


Hi !

See this shortened, simplified example. It is not working, but I don't
understand why...

# Client Process

import os, sys
from subprocess import Popen, PIPE
from time import sleep, time
from cPickle import loads, dumps
from binascii import hexlify, unhexlify
from base64 import encodestring, decodestring
from time import time

def WriteData(WStm,Data):
    data=dumps(Data,1)
    bdata=hexlify(data)
    msg='%s#'%bdata
    WStm.write(msg)

def ReadData(RStm):
    tmpl=[]
    while 1:
        c=RStm.read(1)
        if c=='#':
            break
        tmpl.append(c)
    bdata=''.join(tmpl)
    data=unhexlify(bdata)
    orgdata=loads(data)
    return orgdata

def SubProcessFunctions():
    f=open('spp_clt.log','w')
    print >>f,"m1"
    while 1:
        print >>f,"m2"
        data=ReadData(sys.stdin)
        print >>f,"m3",[data]
        if data=='quit':
            print >>f,"m4"
            WriteData(sys.stdout,'')
            break
        print >>f,"m5"
        WriteData(sys.stdout,'>>%s<<'%[data])
    print >>f,"m6"

if __name__=='__main__':
    SubProcessFunctions()

# The Master process
from spp_clt import ReadData, WriteData
from subprocess import Popen, PIPE
import time

def MasterProcess():
    print "m1"
    p=Popen([r'c:\python24\python.exe','spp_clt.py'], \
     stdin=PIPE,stdout=PIPE)
    print "m2"
    (child_stdout, child_stdin) = (p.stdout, p.stdin)
    print "m3"
    for s in range(2):
        print "m4"
        WriteData(child_stdin,s)
        print "m5"
        ReadData(child_stdout)
        print "m6"
    print "m7"
    WriteData(child_stdin,'quit')
    print "m8"
    ReadData(child_stdout)
    print "m9"

MasterProcess()

It is freezed, because I got deadlock. Every process got "Read" state,
and never get back.
What I do wrong ?
I was trying with this packet managing mode:
def WriteData(WStm,Data):
    data=dumps(Data,1)
    bdata=encodestring(data)
    dlen=len(bdata)
    msg='%12d%s'%(dlen,bdata)
    if WStm==None:
        print msg
    else:
        WStm.write(msg)

def ReadData(RStm):
    dlen=int(RStm.read(12))
    bdata=RStm.read(dlen)
    data=decodestring(bdata)
    orgdata=loads(data)
    return orgdata

but it also freezed.
Why the master doesn't got the packets are sended by client ?

Thanks for your help:
dd




2006/6/21, Dara Durum <durumdara at gmail.com>:
> Hi !
> Sorry, but I need "multios" application...
>
> This version for text mode is working:
>
> import subprocess
> import os,sys
>
> if 'C' in sys.argv:
>     #sys.stdout=open(r'c:\tpp2client.log','w')
>     print "clt start"
>     while 1:
>         head=sys.stdin.read(4)
>         print "clt head",[head]
>         if head.lower()=='quit':
>             break
>         dsize=int(head)
>         if dsize:
>             data=sys.stdin.read(dsize)
>             print "clt get data",len(data)
>     print "clt end\n"
> else:
>     print "MS"
>     p=subprocess.Popen([r'c:\python24\python.exe','tpp2.py','C'], \
>      stdin=subprocess.PIPE,stdout=subprocess.PIPE)
>     print "MSS"
>     (child_stdout, child_stdin) = (p.stdout, p.stdin)
>     data=range(1000)
>     import cPickle
>     bdata=cPickle.dumps(data,1)
>     print len(bdata)
>     import binascii
>     hdata=binascii.hexlify(bdata)
>     print len(hdata)
>     import base64
>     hdata=base64.encodestring(bdata)
>     print len(hdata)
>     child_stdin.write('%04d'%len(hdata))
>     child_stdin.write(hdata)
>     child_stdin.write('quit')
>     output=child_stdout.readlines()
>     print "Client's answer:\n",'<'*80
>     for s in output:
>         print s.strip()
>     print '>'*80
>     print "MEE"
>
> I will see your idea: the bittorrent...
>
> Thanx:
> dd
>
>
>
> 2006/6/20, Daniel Dittmar <daniel.dittmar at sap.corp>:
> > Dara Durum wrote:
> > > Now I trying with packet size decreasing. Are PIPE-s can handle the
> > > binary data packets, or I need to convert them with base64 ?
> >
> > In the client, you need to set the mode of sys.stdin to binary,
> > otherwise, you get the DOS translation of linefeeds. See
> > http://mail.python.org/pipermail/python-list/2000-January/020463.html
> > for some hints.
> >
> > I assume that the stream returned by subprocess.popen are also opened in
> > text mode and you have to change them tobinary, see the second hint in
> > the link mentioned above.
> >
> > Daniel
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: spp_mast.py
Type: text/x-python
Size: 582 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060626/15d2d889/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: spp_clt.py
Type: text/x-python
Size: 1162 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060626/15d2d889/attachment-0001.py>


More information about the Python-list mailing list