473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Sharing Pipes in win32

In windows platforms how can I make two processes hold two ends of the same pipe?



os.pipe() returns reader and writer descriptors but how do I export one of the ends to another process?



Note: popen() is not good for me since the new process is left without tty input and output.




Jul 18 '05 #1
2 2859
Nir of the Waves wrote:
In windows platforms how can I make two processes hold two ends of the
same pipe?

os.pipe() returns reader and writer descriptors but how do I export one
of the ends to another process?

Note: popen() is not good for me since the new process is left without
tty input and output.


I believe you can either do it via a child process and handle
inheritance (meaning os.* functions should work too), or via named pipes
(meaning you will need to use win32process).

Mark.

Jul 18 '05 #2
Nir of the Waves wrote:
In windows platforms how can I make two processes hold two ends of the
same pipe?

os.pipe() returns reader and writer descriptors but how do I export one
of the ends to another process?

Note: popen() is not good for me since the new process is left without
tty input and output.


I have a little namedpipe class that I use. I stole it somewhat from a
perl version a while ago.

"""Named pipe wrapper.

#server
from NamedPipe import NamedPipe
pipe = NamedPipe("mypipe")
pipe.connect()
pipe.write("foo")

#client
from NamedPipe import NamedPipe, CLIENT
import time
pipe = NamedPipe("\\\\.\\pipe\\test1", mode=CLIENT)
while pipe.select() is None:
time.sleep(1)
print pipe.read()

"""
from win32pipe import *
from win32file import *
import pywintypes
import win32security, win32file
import string

SERVER = 0
CLIENT = 1
class NamedPipe:
_header = '\\\\.\\pipe\\'
def __init__(self,
pipeName,
mode=SERVER,
openMode=PIPE_ACCESS_DUPLEX,
pipeMode=0,
maxInstances=1,
outBufferSize=0,
inBufferSize=0,
defaultTimeOut=0,
securityAttributes=None):
# fix the name if appropriate
pipeName = self._fixName(pipeName)

sAttrs = win32security.SECURITY_ATTRIBUTES()
sAttrs.bInheritHandle = 1

self._pipeName = pipeName
# allocate a 100K readbuffer
self._bufsize = 100000
self._buf = win32file.AllocateReadBuffer(self._bufsize)

if mode:
WaitNamedPipe(pipeName, NMPWAIT_WAIT_FOREVER)
self._pipe = CreateFile(pipeName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
None,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
None)

else:
self._pipe = CreateNamedPipe(pipeName, openMode, pipeMode,
maxInstances,
outBufferSize, inBufferSize,
defaultTimeOut, securityAttributes)

def __del__(self):
if self._pipe:
try:
self.disconnect()
except pywintypes.error:
# I don't know why this happens...
pass

def _fixName(self, pipeName):
"""(pipeName)->fixed pipeName
Pipe names must start with header '\\.\pipe\',
if the given pipeName
does not, append the header to the pipeName"""
if string.find(pipeName, self._header) != 0:
return "%s%s"%(self._header, pipeName)
else:
return pipeName

def connect(self):
return ConnectNamedPipe(self._pipe, None)

def disconnect(self):
result = DisconnectNamedPipe(self._pipe)
self._pipe = None
return result

def select(self):
"""returns None is the pipe has no data, self otherwise"""
data, size, something = PeekNamedPipe(self._pipe, 1)
if not data:
return None
return data

def write(self, data):
win32file.WriteFile(self._pipe, data, None)

def read(self):
length, s = win32file.ReadFile(self._pipe, self._buf)
return s

if __name__ == "__main__":
import tempfile
import thread, time
done = 0
def runpipe():
pipe = NamedPipe("test1")
pipe.connect()
pipe.write('Sending to pipe')
def readpipe():
global done
pipe2 = NamedPipe("test1", mode=CLIENT)
print pipe2.read()
done = 1

thread.start_new_thread(runpipe, ())
thread.start_new_thread(readpipe, ())
while not done:
time.sleep(1)


Jul 18 '05 #3

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

Similar topics

3
by: Jane Austine | last post by:
I need to control a command line program via python. I first tried popen2 and 3 but I couldn't find a way to talk to the subprocess interactively; that is, read some and then write some, and...
2
by: Tim Black | last post by:
In my recent experience, popen os pipes always fail when cwd is a UNC path. Can anyone shed any light on this? Although I've seen lots of UNC path-related problems in this newsgroup, I've not been...
6
by: calmar | last post by:
Hi all, I would like to use python for a replacement for some binutils. I would like to be able to pipe things into python. Actually I would not like writing a 'script' to handle the input, but...
7
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h>...
9
by: Hans J?rg Brinksmeyer | last post by:
Hi, does anyone have an idea for this problem: I use anonymous pipes to steer a console program under Win2000 with a second 'steering aplication'. The stdin and output are redirected to...
6
by: Sajid Saeed | last post by:
Hi All, I wuld like to know if there is any possibility of sharing a common class between different applications. i.e. if the two applications are running, they can share the class, and changes...
4
by: Ken Allen | last post by:
Is there any built-in facility for handling named pipes in C#/.Net, or must one use unsafe code to access the WIN32 API directly? There exists some code that uses named pipes heavily and there...
0
by: Mustafa Ahmad Malik | last post by:
Hello, I have wrapped the Win32 pipes function in C#. I have created named pipe with PIPE_WAIT in pipemode parameter like this pipehandle = CreateNamedPipe(_pipeName, PIPE_ACCESS_DUPLEX,...
4
by: mkillian | last post by:
I am having an issue to where I need to share memory between an ASP.Net solution and a VB.Net solution. I have several large arrays and collections that are updated every half second. The data is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.