Threading and TCP server

Jp Calderone exarkun at intarweb.us
Thu Feb 6 13:24:58 EST 2003


On Thu, Feb 06, 2003 at 09:17:27AM -0800, Ehab Teima wrote:
> Hello,
> 
> I am getting started with using threads and server. I wrote the server
> that waits forever until it gets called from the client. The client
> would send a command to the server to be executed on the server
> machine(sort of rsh but with piping between the client). The server
> would handle the client in a separate thread. The code works fine if
> the command to be executed is short. The problem I have is when the
> client passes something like "ls && sleep 60", the server can not
> respond to any other client until it releases this client. Is there a
> way to make the server alert without disconnecting the client that
> casues the long delays.

  At least two options exist:

    Extend your use of threads to include whatever sys call (I'm assuming
os.system()) you're passing commands to.  That is, call os.system("sleep
60") from a separate thread from the main server.

    Use os.popen() or os.openpty() to execute the commands, and then handle
the resulting file objects asynchronously.


  Stepping back from the specifics and looking at the problem as a whole,
I'd recommend using something like Twisted - http://www.twistedmatrix.com/ -
to solve this problem.  It handles the process abstraction for you, it
handles multiplexing clients for you, it does all the socket code, and it
doesn't even rely on threads.


  Jp

-- 
"Pascal is Pascal is Pascal is dog meat."
                -- M. Devine and P. Larson, Computer Science 340
-- 
 up 52 days, 21:50, 2 users, load average: 0.40, 0.45, 0.37
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030206/99adcfcf/attachment.sig>


More information about the Python-list mailing list