HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
PollerManager.py
Go to the documentation of this file.
1 '''
2 Created on Feb 26, 2014
3 
4 @author: igor, bgv
5 '''
6 
7 import zmq
8 
9 from transport.Connection import ConnectionTimeout
10 import app.Utils as Utils # pylint: disable=F0401
11 
12 
13 # Logger initialization
15 
16 
17 # #Class wraps all routine operation of working with zmq.Poller object
18 #
19 class PollerManager(object):
20 
21 
22  def __init__(self, poller=None):
23  if poller is None:
24  self.zmq_poller = zmq.Poller() # pylint: disable=E1101
25  else:
26  self.zmq_poller = poller
27  self.connections = dict()
28 
29  def add(self, connection, name):
30  if hasattr(connection, "zmq_socket"):
31  self.zmq_poller.register(connection.zmq_socket, zmq.POLLIN) # pylint: disable=E1101
32  self.connections[connection.zmq_socket] = name
33 
34 
35  def remove(self, connection):
36  if hasattr(connection, "zmq_socket"):
37  self.zmq_poller.unregister(connection.zmq_socket)
38  del self.connections[connection.zmq_socket]
39 
40 
41  # #poll the zmq_poller
42  #
43  # @return if timeout - raise ConnectionTimeout or
44  # return list of ready connections(names)
45  def poll(self, timeout):
46  socks = dict(self.zmq_poller.poll(timeout))
47  if len(socks) == 0:
48  raise ConnectionTimeout("PollerManager")
49  ready_connection_names = list()
50  for sock in socks:
51  ready_connection_names.append(self.connections[sock])
52  return ready_connection_names
def remove(self, connection)
def add(self, connection, name)
def __init__(self, poller=None)