multiprocessing and accessing server's stdout
Bryan
bryanjugglercryptographer at yahoo.com
Wed Jun 2 19:00:44 EDT 2010
I wrote:
> So what you really need is to capture the output of a command, in this
> case LaTeX, so you can copy it back to the client. You can do that
> with the subprocess module in the Python standard library.
>
> If the command generated so much output so fast that you felt the need
> to avoid the extra copy, I suppose you could fork() then hook stdout
> directly to socket connected to the client with dup2(), then exec()
> the command. But no need for that just to capture LaTeX's output.
Upon further reading, I see that the subprocess module makes the
direct-hookup method easy, at least on 'nix systems. Just tell
subprocess.Popen to use the client-connected socket as the
subprocess's stdout.
The question here turns out to make more sense than I had though upon
reading the first post. The server runs a command at the client's
request, and we want to deliver the output of that command back to the
client. A brilliantly efficient method is to direct the command's
stdout to the client's connection.
Below is a demo server that sends the host's words file to any client
that connects. It assumes Unix.
--Bryan Olson
#!/usr/bin/python
from thread import start_new_thread
from subprocess import Popen
def demo(sock):
subp = Popen(['cat', '/usr/share/dict/words'], stdout=sock)
subp.wait()
sock.close()
if __name__ == '__main__':
listener_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener_sock.bind(('', 54321))
listener_sock.listen(5)
while True:
sock, remote_address = listener_sock.accept()
start_new_thread(demo, (sock,))
More information about the Python-list
mailing list