RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
ServiceControllerAsync.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
5 # Copyright 2015 RAPP
6 
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 
11  #http://www.apache.org/licenses/LICENSE-2.0
12 
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 
19 # Authors: Konstantinos Panayiotou, Manos Tsardoulias
20 # contact: klpanagi@gmail.com, etsardou@iti.gr
21 
22 
23 ## @file ServiceController/ServiceControllerSync.py
24 #
25 # @copyright Rapp Projecty EU 2015
26 # @author Konstantinos Panayiotou, [klpanagi@gmail.com]
27 #
28 
29 
30 from ServiceControllerBase import *
31 
32 # high-level interface for asynchronously executing callables.
33 from concurrent.futures import ThreadPoolExecutor, as_completed
34 
35 
36 
38  """ Synchronous service controller class implementation. """
39 
40  def __init__(self, service, max_workers=8):
41  """! Constructor
42 
43  @param service Service - Service instance.
44  @param max_workers - Number of maximum workers to spawn
45  """
46  # Create a thread pool manager
47  self.__threadPool = ThreadPoolExecutor(max_workers=max_workers)
48 
49  super(ServiceControllerAsync, self).__init__(service)
50 
51 
52  def run_job(self, msg, url, clb=None):
53  """! Run the service
54 
55  Submit callback function to the worker thread and return the future.
56  @param clb Function - Callback function to execute on arrival of
57  the response.
58 
59  @returns _future - The future.
60  """
61  _future = self.__threadPool.submit(self._worker_exec, msg, url, clb=clb)
62  return _future
63 
64 
65  def _worker_exec(self, msg, url, clb=None):
66  # Unpack payload and file objects from cloud service object
67  payload = msg.req.make_payload()
68  files = msg.req.make_files()
69 
70  if self._service.persistent:
71  resp = self._post_persistent(url, payload, files)
72  else:
73  resp = self._post_session_once(url, payload, files)
74 
75  for key, val in resp.iteritems():
76  msg.resp.set(key, val)
77  if clb is not None:
78  clb(msg.resp)
79  return msg.resp
def _post_persistent
Post Request using active session - persistent connection.
def _post_session_once
Post Request while initiating a new session.