Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Are there) PERFORMANCE advantages of python socketserver over regular socket object?

thanks for the interesting responses thus far. In light of said responses I have changed my question a bit.

guess what I really need to know is, is socketserver as opposed to the straight-up socket library designed to handle both periods of latency and stress, i.e. does it have additional mechanisms or features that justify its implicitly advertised status as a "server," or is it just slightly easier to use?

everyone seems to be recommending socketserver but I'm still not entirely clear why, as opposed to socket.

thanks!!!

I've built some server programs in python based on the standard socket library http://docs.python.org/library/socket.html

I've noticed that they seem to work just fine except that without load they have a tendency to go to sleep after a while. I guess this may not be an issue in production (no doubt there will be plenty of other issues) but I would like to know if I am using the right code for the job here.

Looking around I saw that python also provides a socketserver library - http://docs.python.org/library/socketserver.html

The socket library provides the ability to listen for multiple connections, typically up to 5.

According to the socketserver page, its services are synchronous, i.e. blocking, but one may support asynchronous behavior via threading. I did notice it has the ability to maintain a request queue, with a default value of up to 5 requests...so maybe not much difference there.

I have also read that Twisted runs socketserver under the hood. Though I would rather not get into a beast the size of Twisted unless it's going to be worthwhile.

so my question is, is socketserver more robust than socket? If so, why?

(And how do you know?)

incidentally, is socketserver built on top of python's socket or is it entirely separate?

finally, as a bonus if anyone knows what one could do wrong such that standard sockets 'fall asleep' please feel free to chime in on that too.

Oh, and I'm talking python 2.x rather than 3.x here if that makes a difference.

thanks folks!

jsh


Well, I don't have a technical answer but I've implemented SocketServer per folks' recommendations and it IS definitely more reliable. If anyone ever comes up with the low-level explanation please let me know...thanks!

like image 641
jsh Avatar asked Jun 30 '11 19:06

jsh


People also ask

What is python SocketServer module?

The SocketServer module is a framework for creating network servers. It defines classes for handling synchronous network requests (the server request handler blocks until the request is completed) over TCP, UDP, Unix streams, and Unix datagrams.

Why we use socket programming in python?

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection.

What is Serve_forever python?

serve_forever() An alternative request handler class that makes use of streams (file-like objects that simplify communication by providing the standard file interface): class MyTCPHandler(socketserver.

How do you socket a server in python?

Python Socket Server To use python socket connection, we need to import socket module. Then, sequentially we need to perform some task to establish connection between server and client. We can obtain host address by using socket. gethostname() function.


1 Answers

The socket module is a very low-level module for sending and receiving packets. As said in the documentation, it "provides access to the BSD socket interface".

If you want something more elaborate, there is "socketserver" that takes care of the gory details for you, but it is still relatively low level.

On top of that you can find an HTTP server, with or without CGI, an XML-RPC server, and so on. These are frameworks, which usually means that their code calls your code. It makes things simpler because you just have to fill some "gaps" to have a fully working server, but it also means you have a little bit less control over what it does.

If you only need features of socketserver, I would probably go with it, unless you want to reinvent the wheel for some reason (and there are always good reasons to design new wheels, for example to understand how it works).

like image 140
ascobol Avatar answered Oct 10 '22 16:10

ascobol