[zeromq-dev] Client Server Model in Python

Oliver Smith oliver at kfs.org
Tue Aug 24 18:56:31 CEST 2010

Abhishek K said the following on 8/24/2010 8:05 AM:
> Hi
> I am new to ZeroMQ
> I am using a Client Server Model in Python.
> This is the basic outline of the server code.
> context=zmq.Context()
> socket=context.socket(zmq.REP)
> socket.bind('tcp:// <>')
> while True:
>     message=socket.recv()
>     if message[:2]=="01":
>     ##logic1
>     if messafe[:2]=="02":
>       ## logic 2
>     socket.send(response)
> I have 2 questions,
>     * I think the if the message processing logic takes lot of time,
>       how can I thread the process of processing the message (in Python)
>     * I need to send an options along with the message, currently I am
>       sending the options as the first  2 characters of the message.
>       (message[:]). Is there a better way provided by ZMQ.
Bear in mind that Python's default threading system isn't true 
threading... Your first option might be to create separate threads with 
different endpoints for different message types. You could maybe look at 
Stackless python for the threading.

You could use the multipart message scheme for splitting the options 
from the message, and then use a lookup table to decide what function to 
call to process the second half of the message.

     handlers = {
         '01' : handle01
     ,    '02': handle02

     def handle01(msg):

     def handle02(msg):

         msg = sock.recv()
         prefix = msg[:2]
         if not prefix in handlers:

- Oliver

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100824/bea7f8cd/attachment.htm>

More information about the zeromq-dev mailing list