[zeromq-dev] Client Server Model in Python

Abhishek K abhishek.kona at gmail.com
Tue Aug 24 19:00:00 CEST 2010


The method I was looking for was

class Extended_thread(Threading.thread):
        def run(a)
          #some logic

context=zmq.Context()
socket=context.socket(zmq.REP)
socket.bind('tcp://127.0.0.1:5555')
while True:
    message=socket.recv()
           new Extended_thread(socket)




Abhishek Kona
Department of Computer Engineering
National Institute of Technology
Karnataka, India


On Tue, Aug 24, 2010 at 10:26 PM, Oliver Smith <oliver at kfs.org> wrote:

>  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://127.0.0.1:5555')
> 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):
>         ...
>
>     main():
>         ...
>         msg = sock.recv()
>         prefix = msg[:2]
>         if not prefix in handlers:
>             error(msg)
>         else:
>             handlers[prefix](msg[2:])
>
> - Oliver
>
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100824/8e8e75c6/attachment.htm>


More information about the zeromq-dev mailing list