~ubuntu-branches/ubuntu/precise/pyzmq/precise

« back to all changes in this revision

Viewing changes to examples/device/server.py

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ożarowski
  • Date: 2011-02-15 09:08:36 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110215090836-phh4slym1g6muucn
Tags: 2.0.10.1-2
* Team upload.
* Upload to unstable
* Add Breaks: ${python:Breaks}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""A device based server."""
 
2
 
 
3
#
 
4
#    Copyright (c) 2010 Brian E. Granger and Eugene Chernyshov
 
5
#
 
6
#    This file is part of pyzmq.
 
7
#
 
8
#    pyzmq is free software; you can redistribute it and/or modify it under
 
9
#    the terms of the Lesser GNU General Public License as published by
 
10
#    the Free Software Foundation; either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    pyzmq is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    Lesser GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the Lesser GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
import zmq
 
22
import os
 
23
import threading
 
24
import time
 
25
 
 
26
print 'Server', os.getpid()
 
27
 
 
28
def routine(context):
 
29
    socket = context.socket(zmq.REP)
 
30
 
 
31
    socket.connect("inproc://workers")
 
32
 
 
33
    while True:
 
34
        message = socket.recv()
 
35
        time.sleep(1)
 
36
        socket.send(message)
 
37
 
 
38
context = zmq.Context(1)
 
39
 
 
40
workers = context.socket(zmq.XREQ)
 
41
workers.bind("inproc://workers");
 
42
 
 
43
clients = context.socket(zmq.XREP)
 
44
clients.bind('tcp://127.0.0.1:5555')
 
45
 
 
46
for i in range(10):
 
47
    thread = threading.Thread(target=routine, args=(context, ))
 
48
    thread.start()
 
49
 
 
50
zmq.device(zmq.QUEUE, clients, workers)
 
51
 
 
52
print "Finished"