CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions
HTTP.RequestManager Class Reference

Public Member Functions

def __init__ (self, num_connections=10, ssl_opts=None, user_agent=None, request_headers=None, request_init=None, request_respond=None, request_error=None, handle_init=None)
 
def process (self)
 
def put (self, task)
 

Public Attributes

 cm
 
 free
 
 handles
 
 queue
 
 request_error
 
 request_init
 
 request_respond
 

Private Member Functions

def _request_error (self, c, task, errmsg, errno)
 
def _request_init (self, c, url)
 
def _request_respond (self, args)
 

Detailed Description

Manager of multiple concurrent or overlapping HTTP requests.

This is a utility class acting as a pump of several overlapping
HTTP requests against any number of HTTP or HTTPS servers. It
uses a configurable number of simultaneous connections, ten by
default. The actual connection layer is handled using curl, and
the client classes need to aware of this to a limited degree.

The client supplies optional callback methods for initialising,
responding and handling errors on connections. At the very least
the request response callback should be defined.

This class is not designed for multi-threaded use. It employs
overlapping requests, but in a single thread. Only one thread
at a time should be calling `process()`; several threads may
call `.put()` provided the caller uses a mutex so that only one
thread calls into the method at a time.

Definition at line 4 of file HTTP.py.

Constructor & Destructor Documentation

def HTTP.RequestManager.__init__ (   self,
  num_connections = 10,
  ssl_opts = None,
  user_agent = None,
  request_headers = None,
  request_init = None,
  request_respond = None,
  request_error = None,
  handle_init = None 
)
Initialise the request manager. The arguments are:

:arg num_connections: maximum number of simultaneous connections.
:arg ssl_opts: optional SSLOptions (Monitoring.Core.X509) for SSL
X509 parametre values, e.g. for X509 client authentication.
:arg user_agent: sets user agent identification string if defined.
:arg request_headers: if defined, specifies list of additional HTTP
request headers to be added to each request.
:arg request_init: optional callback to initialise requests; the
default assumes each task is a URL to access and sets the `URL`
property on the curl object to the task value.
:arg request_respond: callback for handling responses; at the very
minimum this should be defined as the default one does nothing.
:arg request_error: callback for handling connection errors; the
default one raises a RuntimeException.
:arg handle_init: callback for customising connection handles at
creation time; the callback will be invoked for each connection
object as it's created and queued to the idle connection list.

Definition at line 26 of file HTTP.py.

26  request_error = None, handle_init = None):
27  """Initialise the request manager. The arguments are:
28 
29 :arg num_connections: maximum number of simultaneous connections.
30 :arg ssl_opts: optional SSLOptions (Monitoring.Core.X509) for SSL
31 X509 parametre values, e.g. for X509 client authentication.
32 :arg user_agent: sets user agent identification string if defined.
33 :arg request_headers: if defined, specifies list of additional HTTP
34 request headers to be added to each request.
35 :arg request_init: optional callback to initialise requests; the
36 default assumes each task is a URL to access and sets the `URL`
37 property on the curl object to the task value.
38 :arg request_respond: callback for handling responses; at the very
39 minimum this should be defined as the default one does nothing.
40 :arg request_error: callback for handling connection errors; the
41 default one raises a RuntimeException.
42 :arg handle_init: callback for customising connection handles at
43 creation time; the callback will be invoked for each connection
44 object as it's created and queued to the idle connection list."""
45  self.request_respond = request_respond or self._request_respond
46  self.request_error = request_error or self._request_error
47  self.request_init = request_init or self._request_init
48  self.cm = CurlMulti()
49  self.handles = [Curl() for i in xrange(0, num_connections)]
50  self.free = [c for c in self.handles]
51  self.queue = []
52 
53  for c in self.handles:
54  c.buffer = None
55  c.setopt(NOSIGNAL, 1)
56  c.setopt(TIMEOUT, 300)
57  c.setopt(CONNECTTIMEOUT, 30)
58  c.setopt(FOLLOWLOCATION, 1)
59  c.setopt(MAXREDIRS, 5)
60  if user_agent:
61  c.setopt(USERAGENT, user_agent)
62  if ssl_opts:
63  c.setopt(CAPATH, ssl_opts.ca_path)
64  c.setopt(SSLCERT, ssl_opts.cert_file)
65  c.setopt(SSLKEY, ssl_opts.key_file)
66  if ssl_opts.key_pass:
67  c.setopt(SSLKEYPASSWD, ssl_opts.key_pass)
68  if request_headers:
69  c.setopt(HTTPHEADER, request_headers)
70  if handle_init:
71  handle_init(c)
72 
def _request_init(self, c, url)
Definition: HTTP.py:73
def _request_error(self, c, task, errmsg, errno)
Definition: HTTP.py:77
def _request_respond(self, args)
Definition: HTTP.py:81

Member Function Documentation

def HTTP.RequestManager._request_error (   self,
  c,
  task,
  errmsg,
  errno 
)
private
Default request error callback.

Definition at line 77 of file HTTP.py.

77  def _request_error(self, c, task, errmsg, errno):
78  """Default request error callback."""
79  raise RuntimeError((task, errmsg, errno))
80 
def _request_error(self, c, task, errmsg, errno)
Definition: HTTP.py:77
def HTTP.RequestManager._request_init (   self,
  c,
  url 
)
private
Default request initialisation callback.

Definition at line 73 of file HTTP.py.

73  def _request_init(self, c, url):
74  """Default request initialisation callback."""
75  c.setopt(URL, url)
76 
def _request_init(self, c, url)
Definition: HTTP.py:73
def HTTP.RequestManager._request_respond (   self,
  args 
)
private
Default request response callback.

Definition at line 81 of file HTTP.py.

81  def _request_respond(self, *args):
82  """Default request response callback."""
83  pass
84 
def _request_respond(self, args)
Definition: HTTP.py:81
def HTTP.RequestManager.process (   self)
Process pending requests until none are left.

This method processes all requests queued with `.put()` until they
have been fully processed. It calls the ``request_respond`` callback
for all successfully completed requests, and ``request_error`` for
all failed ones.

Any new requests added by callbacks by invoking ``put()`` are also
processed before returning.

Definition at line 90 of file HTTP.py.

References HTTP.RequestManager.free, HTTP.RequestManager.queue, HTTP.RequestManager.request_error, HTTP.RequestManager.request_init, and HTTP.RequestManager.request_respond.

Referenced by ConfigBuilder.ConfigBuilder.addExtraStream(), ConfigBuilder.ConfigBuilder.completeInputCommand(), ConfigBuilder.ConfigBuilder.doNotInlineEventContent(), Vispa.Plugins.ConfigEditor.ConfigDataAccessor.ConfigDataAccessor.dumpPython(), ConfigBuilder.ConfigBuilder.PrintAllModules.leave(), Vispa.Plugins.ConfigEditor.ConfigDataAccessor.ConfigDataAccessor.open(), Vispa.Plugins.ConfigEditor.ConfigDataAccessor.ConfigDataAccessor.outputEventContent(), ConfigBuilder.ConfigBuilder.prepare(), ConfigBuilder.ConfigBuilder.prepare_ALCA(), ConfigBuilder.ConfigBuilder.prepare_DQM(), ConfigBuilder.ConfigBuilder.prepare_HLT(), ConfigBuilder.ConfigBuilder.prepare_LHE(), ConfigBuilder.ConfigBuilder.prepare_PATFILTER(), ConfigBuilder.ConfigBuilder.prepare_VALIDATION(), ConfigBuilder.ConfigBuilder.renameHLTprocessInSequence(), ConfigBuilder.ConfigBuilder.renameInputTagsInSequence(), ConfigBuilder.ConfigBuilder.scheduleSequence(), Vispa.Plugins.ConfigEditor.ConfigDataAccessor.ConfigDataAccessor.setProcess(), and Vispa.Plugins.ConfigEditor.ConfigDataAccessor.ConfigDataAccessor.setProperty().

90  def process(self):
91  """Process pending requests until none are left.
92 
93 This method processes all requests queued with `.put()` until they
94 have been fully processed. It calls the ``request_respond`` callback
95 for all successfully completed requests, and ``request_error`` for
96 all failed ones.
97 
98 Any new requests added by callbacks by invoking ``put()`` are also
99 processed before returning."""
100  npending = 0
101  while self.queue or npending:
102  while self.queue and self.free:
103  c = self.free.pop()
104  c.task = self.queue.pop(0)
105  c.buffer = b = StringIO()
106  c.setopt(WRITEFUNCTION, b.write)
107  self.request_init(c, *c.task)
108  self.cm.add_handle(c)
109  npending += 1
110 
111  while True:
112  ret, nhandles = self.cm.perform()
113  if ret != E_CALL_MULTI_PERFORM:
114  break
115 
116  while True:
117  numq, ok, err = self.cm.info_read()
118 
119  for c in ok:
120  assert npending > 0
121  self.cm.remove_handle(c)
122  self.request_respond(c)
123  c.buffer = None
124  self.free.append(c)
125  npending -= 1
126 
127  for c, errno, errmsg in err:
128  assert npending > 0
129  self.cm.remove_handle(c)
130  self.free.append(c)
131  npending -= 1
132  self.request_error(c, c.task, errmsg, errno)
133 
134  if numq == 0:
135  break
136 
137  self.cm.select(1.)
138 
def HTTP.RequestManager.put (   self,
  task 
)
Add a new task. The task object should be a tuple and is
passed to ``request_init`` callback passed to the constructor.

Definition at line 85 of file HTTP.py.

85  def put(self, task):
86  """Add a new task. The task object should be a tuple and is
87 passed to ``request_init`` callback passed to the constructor."""
88  self.queue.append(task)
89 
def put(self, task)
Definition: HTTP.py:85

Member Data Documentation

HTTP.RequestManager.cm

Definition at line 48 of file HTTP.py.

HTTP.RequestManager.free

Definition at line 50 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

HTTP.RequestManager.handles

Definition at line 49 of file HTTP.py.

Referenced by SimpleMuonAnalyzer.SimpleMuonAnalyzer.declareHandles(), ZEleEleAnalyzer.ZEleEleAnalyzer.declareHandles(), ZMuMuAnalyzer.ZMuMuAnalyzer.declareHandles(), TriggerAnalyzer.TriggerAnalyzer.declareHandles(), MetAnalyzer.MetAnalyzer.declareHandles(), SimpleJetAnalyzer.SimpleJetAnalyzer.declareHandles(), objects.TauAnalyzer.TauAnalyzer.declareHandles(), core.TriggerBitFilter.TriggerBitFilter.declareHandles(), core.TriggerMatchAnalyzer.TriggerMatchAnalyzer.declareHandles(), core.TriggerBitAnalyzer.TriggerBitAnalyzer.declareHandles(), RazorAnalyzer.RazorAnalyzer.declareHandles(), objects.METAnalyzer.METAnalyzer.declareHandles(), AlphaTAnalyzer.AlphaTAnalyzer.declareHandles(), MT2Analyzer.MT2Analyzer.declareHandles(), core.AutoFillTreeProducer.AutoFillTreeProducer.declareHandles(), objects.PhotonAnalyzer.PhotonAnalyzer.declareHandles(), JetAnalyzer.JetAnalyzer.declareHandles(), objects.VertexAnalyzer.VertexAnalyzer.declareHandles(), objects.IsoTrackAnalyzer.IsoTrackAnalyzer.declareHandles(), core.PileUpAnalyzer.PileUpAnalyzer.declareHandles(), objects.LeptonAnalyzer.LeptonAnalyzer.declareHandles(), objects.JetAnalyzer.JetAnalyzer.declareHandles(), core.AutoFillTreeProducer.AutoFillTreeProducer.fillTree(), objects.LeptonAnalyzer.LeptonAnalyzer.makeAllElectrons(), objects.LeptonAnalyzer.LeptonAnalyzer.makeAllMuons(), objects.IsoTrackAnalyzer.IsoTrackAnalyzer.makeIsoTrack(), objects.LeptonAnalyzer.LeptonAnalyzer.makeLeptons(), objects.PhotonAnalyzer.PhotonAnalyzer.makePhotons(), objects.TauAnalyzer.TauAnalyzer.makeTaus(), objects.METAnalyzer.METAnalyzer.makeTkMETs(), SimpleMuonAnalyzer.SimpleMuonAnalyzer.process(), SimpleJetAnalyzer.SimpleJetAnalyzer.process(), MetAnalyzer.MetAnalyzer.process(), core.TriggerMatchAnalyzer.TriggerMatchAnalyzer.process(), core.TriggerBitFilter.TriggerBitFilter.process(), TriggerAnalyzer.TriggerAnalyzer.process(), JetAnalyzer.JetAnalyzer.process(), core.TriggerBitAnalyzer.TriggerBitAnalyzer.process(), DiLeptonAnalyzer.DiLeptonAnalyzer.process(), objects.VertexAnalyzer.VertexAnalyzer.process(), core.PileUpAnalyzer.PileUpAnalyzer.process(), objects.JetAnalyzer.JetAnalyzer.process(), objects.PhotonAnalyzer.PhotonAnalyzer.randomCone(), and core.Analyzer.Analyzer.readCollections().

HTTP.RequestManager.queue

Definition at line 51 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

HTTP.RequestManager.request_error

Definition at line 46 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

HTTP.RequestManager.request_init

Definition at line 47 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

HTTP.RequestManager.request_respond

Definition at line 45 of file HTTP.py.

Referenced by HTTP.RequestManager.process().