473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duplex communication with pipes - is possible ?

Hi !

I want to create a Process Pool Object.
I can hold started processes, and can communicate with them.

I tryed with many ipc methods, but every of them have bug or other problem.
Sockets are unavailabe (because Windows Firewall hold them).

I think I will use pipe.

The object's pseudocode:
while not Quit:
CheckProcessOut puts;
ProcessReceived Data;
SendDataToSubPr ocesses;
if NoMoreData: Quit=1

If I used pipes and subprocess module in Windows, I got big freezes,
and deadlocks.

Main proc:
subprocpipe.wri te('aaaa\n')
subprocpipe.rea dlines()

Sub proc:
input=sys.stdin .readlines().st rip()
print input+'!!!'

It is working. But when I move this client code to cycle, I got deadlock.
while not Quit:
input=sys.stdin .readlines().st rip()
print input+'!!!'
Quit=input=='q'

Why ? Why I cannot create cyclic communication with client ?
Subprocess must "staying alive" (don't die), and need to stay in
reuseable state ?

Simply: subprocess pool needed !!!

Thanks for help:
dd
Jun 16 '06 #1
5 2416
Dara Durum wrote:
Hi !

I want to create a Process Pool Object.
I can hold started processes, and can communicate with them.

I tryed with many ipc methods, but every of them have bug or other problem.
Sockets are unavailabe (because Windows Firewall hold them).

I think I will use pipe.

The object's pseudocode:
while not Quit:
CheckProcessOut puts;
ProcessReceived Data;
SendDataToSubPr ocesses;
if NoMoreData: Quit=1

If I used pipes and subprocess module in Windows, I got big freezes,
and deadlocks.

Main proc:
subprocpipe.wri te('aaaa\n')
subprocpipe.rea dlines()

Sub proc:
input=sys.stdin .readlines().st rip()
print input+'!!!'

It is working. But when I move this client code to cycle, I got deadlock.
while not Quit:
input=sys.stdin .readlines().st rip()
print input+'!!!'
Quit=input=='q'

Why ? Why I cannot create cyclic communication with client ?
Subprocess must "staying alive" (don't die), and need to stay in
reuseable state ?

Simply: subprocess pool needed !!!

Thanks for help:
dd


readlines () will try to read until the stream/socket is closed. Try to
read only one line. This of course means that you cannot sent \n as part
of the data, you have to escape them somehow.

I'm not sure how the pipe code is searching for the \n. Trying to read
too much could lead to deadlocks as well. (Although I'm sure that the
code is written to return fewerbytes than requested if there isn't
enough data pending in the pipe). A safer variant might be to transfer a
fixed number of bytes containing the length n of the following data, and
then n bytes containing the actual data.

Daniel
Jun 16 '06 #2

<snip

readlines () will try to read until the stream/socket is closed. Try to
read only one line. This of course means that you cannot sent \n as part
of the data, you have to escape them somehow.

<snip>

If I remember correctly, if you want to pass '\n' so readline won't
stop, you should be able to escape the escape '\\n', then remove the
extra '\' when actually processing.

Of course, readline will continue to read until buffer filled or a real
'\n' is passed (g).

Jun 16 '06 #3
Hi !

Ahhh !

It's working !

This is the simple client code:
if 'C' in sys.argv:
#sys.stdout=ope n(r'c:\tpp2clie nt.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"#,[data]
print "clt end\n"
And the master:
else:
print "MS"
p=subprocess.Po pen([r'c:\python24\p ython.exe','tpp 2.py','C'], \
stdin=subproces s.PIPE,stdout=s ubprocess.PIPE)
print "MSS"
(child_stdout, child_stdin) = (p.stdout, p.stdin)
data=range(1000 )
import cPickle
bdata=cPickle.d umps(data,1)
print len(bdata)
import binascii
hdata=binascii. hexlify(bdata)
print len(hdata)
import base64
hdata=base64.en codestring(bdat a)
print len(hdata)
child_stdin.wri te('%04d'%len(h data))
child_stdin.wri te(hdata)
child_stdin.wri te('quit')
#child_stdin.wr ite('quit')
#child_stdin.wr ite('quit\n')
output=child_st dout.readlines( )
print [output]
print "MEE"

Now I trying with packet size decreasing. Are PIPE-s can handle the
binary data packets, or I need to convert them with base64 ?

Thanks for your help:
dd
Jun 20 '06 #4
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/pyt...ry/020463.html
for some hints.

I assume that the stream returned by subprocess.pope n are also opened in
text mode and you have to change them tobinary, see the second hint in
the link mentioned above.

Daniel
Jun 20 '06 #5
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(d ata)
msg='%s#'%bdata
WStm.write(msg)

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

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

if __name__=='__ma in__':
SubProcessFunct ions()

# 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\p ython.exe','spp _clt.py'], \
stdin=PIPE,stdo ut=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=encodestr ing(data)
dlen=len(bdata)
msg='%12d%s'%(d len,bdata)
if WStm==None:
print msg
else:
WStm.write(msg)

def ReadData(RStm):
dlen=int(RStm.r ead(12))
bdata=RStm.read (dlen)
data=decodestri ng(bdata)
orgdata=loads(d ata)
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 <du*******@gmai l.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=ope n(r'c:\tpp2clie nt.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.Po pen([r'c:\python24\p ython.exe','tpp 2.py','C'], \
stdin=subproces s.PIPE,stdout=s ubprocess.PIPE)
print "MSS"
(child_stdout, child_stdin) = (p.stdout, p.stdin)
data=range(1000 )
import cPickle
bdata=cPickle.d umps(data,1)
print len(bdata)
import binascii
hdata=binascii. hexlify(bdata)
print len(hdata)
import base64
hdata=base64.en codestring(bdat a)
print len(hdata)
child_stdin.wri te('%04d'%len(h data))
child_stdin.wri te(hdata)
child_stdin.wri te('quit')
output=child_st dout.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 <da************ @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/pyt...ry/020463.html
for some hints.

I assume that the stream returned by subprocess.pope n 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


Jun 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3264
by: KohlerTommy | last post by:
In my application I need to give the user the ability to print duplex if the selected printer supports duplex printing. Many of the printer options do not make much sense in my application, and many of the settings in the common printer dialog would have a negative impact on my printing process. To handle this strict printing constraint on which my application imposes I do not want to show the common print dialog. I want to be able to...
3
5930
by: james | last post by:
Hallo! How do I realise the communication between two applications that run two diffenrent processes, in a way to exchange some information? Thanks a lot for helping James
10
16297
by: Joe M | last post by:
I was wondering if someone could lend me a hand with a C# problem I am having I am trying to use the “setPrinter†api to change the duplex setting (under printing preferences on printer context menu) so that I can send a document to the printer in duplex mode These are my declarations string pDeviceNameg, IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode) string szPrinter,
3
10885
by: BizzyTalking | last post by:
Would any kindly tell me if its possible to communicate between a service and a form application. For example, retrieve information from the service, like current activity or progress of its operations. In particular, I would like to to monitor more than just whether a service is running. In the system tray I would like to display whether the service is idle, or running a particular method. Is this possible? If so, should I be using...
6
1743
by: el7akika | last post by:
hi i want to communicate between simulator1 and simulator2 I use pipes, but, um, it doesn't seem to work right. The main idea is to have a process(main) simulator1 that creates another(child)simulator2 using CreateProcess(). i launch simulator 2 and establish a communication with him
3
4794
by: James Aguilar | last post by:
Oh wise readers of comp.lang.python, Lend a newbie your ears. I have read several old articles from this group about memory mapping and interprocess communication and have Googled the sh** out of the internet, but have not found sufficient to answer my questions. Suppose that I am writing a ray tracer in Python. Well, perhaps not a ray tracer. Suppose that I am writing a ray tracer that has to update sixty times a second (Ignore...
1
2186
by: dotnetdummy | last post by:
Hi, Can anyone who can help me out if possible. At the moment, I'm working on a window application (vb.net) which print out html document's through a default network printer. I'm using browser control Dim IE As SHDocVw.InternetExplorer) ....
6
6580
by: Kasper Lindberg | last post by:
Hi NG, How to do two-way-interprocess communication using java? Imagine a client/server setup where the client can request a number of functions to be performed by the server. Since the server sometimes pushes messages to the client(s), and no assumption can be made regarding the client and when/how often it is running, a second program (call it CPM) is running on the client machine.
1
3195
by: =?Utf-8?B?SmltbXk=?= | last post by:
Hello, I have a scenario where I have one or more publishers, a broadcaster and one or more subscribers. The publishers publish information to the broadcaster, which then broadcasts that information to all subscribers. Subscribers use callbacks to receive information from the broadcaster. I'm in the process of switching to NetDataContractSerializer, and that seems to work fine for communication from publishers to the broadcaster...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.