Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:52:03+00:00 2026-05-15T00:52:03+00:00

Short version (if you can answer the short version it does the job for

  • 0

Short version (if you can answer the short version it does the job for me, the rest is mainly for the benefit of other people with a similar task):

In python in Windows, I want to create 2 file objects, attached to the same file (it doesn’t have to be an actual file on the hard-drive), one for reading and one for writing, such that if the reading end tries to read it will never get EOF (it will just block until something is written). I think in linux os.mkfifo() would do the job, but in Windows it doesn’t exist. What can be done? (I must use file-objects).

Some extra details:
I have a python module (not written by me) that plays a certain game through stdin and stdout (using raw_input() and print). I also have a Windows executable playing the same game, through stdin and stdout as well. I want to make them play one against the other, and log all their communication.

Here’s the code I can write (the get_fifo() function is not implemented, because that’s what I don’t know to do it Windows):

class Pusher(Thread):
        def __init__(self, source, dest, p1, name):
                Thread.__init__(self)
                self.source = source
                self.dest = dest
                self.name = name
                self.p1 = p1

        def run(self):
                while (self.p1.poll()==None) and\
                      (not self.source.closed) and (not self.source.closed):
                        line = self.source.readline()
                        logging.info('%s: %s' % (self.name, line[:-1]))
                        self.dest.write(line)
                        self.dest.flush()


exe_to_pythonmodule_reader, exe_to_pythonmodule_writer =\
                          get_fifo()
pythonmodule_to_exe_reader, pythonmodule_to_exe_writer =\
                          get_fifo()

p1 = subprocess.Popen(exe, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

old_stdin = sys.stdin
old_stdout = sys.stdout

sys.stdin = exe_to_pythonmodule_reader
sys.stdout = pythonmodule_to_exe_writer

push1 = Pusher(p1.stdout, exe_to_pythonmodule_writer, p1, '1')
push2 = Pusher(pythonmodule_to_exe_reader, p1.stdin, p1, '2')

push1.start()
push2.start()
ret = pythonmodule.play()
sys.stdin = old_stdin
sys.stdout = old_stdout
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:52:04+00:00Added an answer on May 15, 2026 at 12:52 am

    Following the two answers above, I accidentally bumped into the answer. os.pipe() does the job. Thank you for your answers.

    I’m posting the complete code in case someone else is looking for this:

    import subprocess
    from threading import Thread
    import time
    import sys
    import logging
    import tempfile
    import os
    
    import game_playing_module
    
    class Pusher(Thread):
        def __init__(self, source, dest, proc, name):
            Thread.__init__(self)
            self.source = source
            self.dest = dest
            self.name = name
            self.proc = proc
    
        def run(self):
            while (self.proc.poll()==None) and\
                  (not self.source.closed) and (not self.dest.closed):
                line = self.source.readline()
                logging.info('%s: %s' % (self.name, line[:-1]))
                self.dest.write(line)
                self.dest.flush()
    
    def get_reader_writer():
        fd_read, fd_write = os.pipe()
        return os.fdopen(fd_read, 'r'), os.fdopen(fd_write, 'w')
    
    def connect(exe):
        logging.basicConfig(level=logging.DEBUG,\
                            format='%(message)s',\
                            filename=LOG_FILE_NAME,
                            filemode='w')
    
        program_to_grader_reader, program_to_grader_writer =\
                                  get_reader_writer()
    
        grader_to_program_reader, grader_to_program_writer =\
                                  get_reader_writer()
    
        p1 = subprocess.Popen(exe, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)        
    
        old_stdin = sys.stdin
        old_stdout = sys.stdout
    
        sys.stdin = program_to_grader_reader
        sys.stdout = grader_to_program_writer
    
        push1 = Pusher(p1.stdout, program_to_grader_writer, p1, '1')
        push2 = Pusher(grader_to_program_reader, p1.stdin, p1, '2')
    
        push1.start()
        push2.start()
    
        game_playing_module.play()
    
        sys.stdin = old_stdin
        sys.stdout = old_stdout
    
        fil = file(LOG_FILE, 'r')
        data = fil.read()
        fil.close()
        return data
    
    if __name__=='__main__':
        if len(sys.argv) != 2:
            print 'Usage: connect.py exe'
            print sys.argv
            exit()
        print sys.argv
        print connect(sys.argv[1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: Can we read from dozens or hundreds of table partitions in a
In short: How can I use Hg to synchronize repositories between two computers using
In every Visual Studio.NET version you can set keyboard shortcuts using menu Tools ->
Similar questions have been asked before but after a day of going through the
We have customers who are upgrading from one database version to another (Oracle 9i
How can I download page on this link http://www.kayak.com/s/search/air?ai=kayaksample&do=y&ft=ow&ns=n&cb=e&pa=1&l1=ZAG&t1=a&df=dmy&d1=4/10/2010&depart_flex=exact&r1=y&l2=LON&t2=a&d2=11/10/2010&return_flex&r2=y Link changes to short version
Ok a short question: Is there any SIMPLE software with a GUI, that lets
Short story I've got a PHP script filtering incoming mail using a .qmail file.
In short: how to put a zipful of csv data into mysql? Long: I
I have been using SourceMonitor on my project for a couple of years to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.