Documentation for pulsar 0.4.6. For development docs, go here.
Asynchronous application for serving requests on a socket. This is the base class of pulsar.apps.wsgi.WSGIServer.
A simple echo server script.py can be implemented as follow:
import pulsar from pulsar.apps.socket import SocketServer class EchoProtocol: "The simplest protocol possible." def decode(self, data): return bytes(data), bytearray() def encode(self, data): return data def echoserver(actor, socket, **kwargs): kwargs['protocol_factory'] = EchoProtocol return pulsar.AsyncSocketServer(actor, socket, **kwargs) if __name__ == '__main__': SocketServer(socket_server_factory=echoserver).start()
The protocol class needs to implement two methods, the decode for servers and the encode for clients. Both methods accept as input one parameter which can be bytes or a bytesarray.
The decode method must return a two elements tuple of the form:
- None, bytesarray: when more data is needed to form a message.
- message, bytesarray: where message is a correctly parsed message and bytesarray contains data for the next message.
The encode method must return bytes which will be sent to the server.
python script.py --bind 127.0.0.1:8070
To control the concurrency of the server you can use the backlog settings. For example:
python script.py --backlog 1000
will serve a maximum of 1000 clients per pulsar.Worker concurrently.
When running a SocketServer in threading mode:
python script.py --concurrency thread
the number of pulsar.Worker serving the application is set to 0 so that the application is actually served by the Arbiter eventloop (we refer this to a single process server). This configuration is used when debugging, testing, benchmarking or small load servers.
In addition, a SocketServer in multi-process mode is only available for:
- Posix systems
- Windows running python 3.2 or above.
This application bind a socket to a given address and listen for requests. The request handler is constructued from the socket_server_factory parameter/attribute.
Callable which returns the asynchronous socket server, usually a subclass of pulsar.AsyncSocketServer.
The socket address, available once the application has started.