uncoupled threading from making a request

This commit is contained in:
Rudi Grinberg 2012-10-25 11:08:21 -04:00
parent f718070d3c
commit 86b262ed8f
1 changed files with 18 additions and 12 deletions

View File

@ -6,13 +6,23 @@ import copy
from media.monitor.handler import ReportHandler from media.monitor.handler import ReportHandler
from media.monitor.log import Loggable from media.monitor.log import Loggable
from media.monitor.exceptions import BadSongFile from media.monitor.exceptions import BadSongFile
from media.monitor.pure import LazyProperty
from media.monitor.eventcontractor import EventContractor from media.monitor.eventcontractor import EventContractor
from media.monitor.events import EventProxy from media.monitor.events import EventProxy
import api_clients.api_client as ac import api_clients.api_client as ac
class RequestSync(threading.Thread,Loggable):
class ThreadedRequestSync(threading.Thread, Loggable):
def __init__(self, rs):
threading.Thread.__init__(self)
self.rs = rs
self.daemon = True
self.start()
def run(self):
self.rs.run_request()
class RequestSync(Loggable):
""" """
This class is responsible for making the api call to send a request This class is responsible for making the api call to send a request
to airtime. In the process it packs the requests and retries for to airtime. In the process it packs the requests and retries for
@ -26,18 +36,13 @@ class RequestSync(threading.Thread,Loggable):
return self return self
def __init__(self, watcher, requests, apiclient): def __init__(self, watcher, requests, apiclient):
threading.Thread.__init__(self)
self.watcher = watcher self.watcher = watcher
self.requests = requests self.requests = requests
self.apiclient = apiclient self.apiclient = apiclient
self.retries = 1 self.retries = 1
self.request_wait = 0.3 self.request_wait = 0.3
@LazyProperty def run_request(self):
def apiclient(self):
return ac.AirtimeApiClient.create_right_config()
def run(self):
self.logger.info("Attempting request with %d items." % self.logger.info("Attempting request with %d items." %
len(self.requests)) len(self.requests))
# Note that we must attach the appropriate mode to every # Note that we must attach the appropriate mode to every
@ -59,6 +64,8 @@ class RequestSync(threading.Thread,Loggable):
request_event.path) request_event.path)
def make_req(): def make_req():
self.apiclient.send_media_monitor_requests( packed_requests ) self.apiclient.send_media_monitor_requests( packed_requests )
# TODO : none of the shit below is necessary. get rid of it
# eventually
for try_index in range(0,self.retries): for try_index in range(0,self.retries):
try: make_req() try: make_req()
# most likely we did not get json response as we expected # most likely we did not get json response as we expected
@ -76,7 +83,7 @@ class RequestSync(threading.Thread,Loggable):
break break
else: self.logger.info("Failed to send request after '%d' tries..." % else: self.logger.info("Failed to send request after '%d' tries..." %
self.retries) self.retries)
self.watcher.flag_done() self.watcher.flag_done() # poor man's condition variable
class TimeoutWatcher(threading.Thread,Loggable): class TimeoutWatcher(threading.Thread,Loggable):
""" """
@ -217,9 +224,8 @@ class WatchSyncer(ReportHandler,Loggable):
requests = copy.copy(self.__queue) requests = copy.copy(self.__queue)
def launch_request(): def launch_request():
# Need shallow copy here # Need shallow copy here
t = RequestSync.create_with_api_client(watcher=self, t = ThreadedRequestSync( RequestSync.create_with_api_client(
requests=requests) watcher=self, requests=requests) )
t.start()
self.__current_thread = t self.__current_thread = t
self.__requests.append(launch_request) self.__requests.append(launch_request)
self.__reset_queue() self.__reset_queue()