Possible to open a file for shared write mode under Windows?
Chris Colbert
sccolbert at gmail.com
Thu Mar 18 01:31:33 EDT 2010
It's not too difficult:
# subp.py
import sys
import time
for i in range(10):
sys.stdout.write('I am subprocess\n')
sys.stdout.flush() # this may not be necessary
time.sleep(1)
# writer.py
import subprocess
import time
subp = subprocess.Popen(['/usr/bin/python', '-u', './subp.py'],
stdout=subprocess.PIPE)
while True:
time.sleep(1)
print('I am main process.') # write this to one section of the file
if subp.poll() != None: # the subprocess has terminated
break
stdout = subp.stdout.readline()
print(stdout) # write this to the other section
brucewayne at broo:~/Documents$ python ./writer.py
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
I am subprocess
I am main process.
On Thu, Mar 18, 2010 at 12:52 AM, Steve Holden <steve at holdenweb.com> wrote:
> Chris Colbert wrote:
> [top-posting switched to bottom-posting]
> >
> > On Thu, Mar 18, 2010 at 12:20 AM, Gabriel Genellina
> > <gagsl-py2 at yahoo.com.ar <mailto:gagsl-py2 at yahoo.com.ar>> wrote:
> >
> > En Wed, 17 Mar 2010 23:42:46 -0300, <python at bdurham.com
> > <mailto:python at bdurham.com>> escribió:
> >
> >
> > Is there a way to open a file for shared write mode under
> > Windows?
> >
> > I have 2 processes that will write to different regions of this
> > shared file.
> >
> >
> > Using the pywin32 package at sourceforge, you have access to the
> > CreateFile function:
> > http://msdn.microsoft.com/en-us/library/aa363858(v=VS.85).aspx
> > You want dwShareMode=FILE_SHARE_WRITE
> >
> > you could also just have one process do the writing for both processes
> > via pipes.
>
> You could, but then you'd have to work out how to avoid blocking on one
> pipe when data was available form the other. Or is this trivially easy,
> and I just don't know enough about pipes?
>
> regards
> Steve
> --
> Steve Holden +1 571 484 6266 +1 800 494 3119
> See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
> Holden Web LLC http://www.holdenweb.com/
> UPCOMING EVENTS: http://holdenweb.eventbrite.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100318/4d98afaa/attachment-0001.html>
More information about the Python-list
mailing list