Last active
August 29, 2015 14:14
-
-
Save slotlocker2/c97c7b4b518177ac36c7 to your computer and use it in GitHub Desktop.
Service rate bottlenecks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import threading | |
from random import randint | |
class ServiceClass: | |
def __init__(self, name, rate): | |
self.name = name #Instance as no __name__ method. | |
self.rate = rate # requests served per second. | |
## Change 60 to 1 to prep up the rate to per second. | |
self.tps = 60/self.rate # time per request. | |
self.last_accessed = [] | |
def service_routine(self,*args): | |
current_time = time.time() | |
# first pass | |
if not self.last_accessed: | |
self.last_accessed.append(0) | |
time_elapsed = current_time - self.last_accessed[0] | |
if time_elapsed < self.tps: | |
wait_time = self.tps - time_elapsed | |
return wait_time # This is the service telling the app that it is not yet ready to accept requests. | |
else: | |
print time.strftime("%H:%M:%S") + " Processing request. Hello from " + self.name + " and the argument is " + str(args[0]) | |
self.last_accessed[0] = current_time | |
return 0 | |
def fetch_data(): | |
''' This function actively logs requests to respective job queues of the services''' | |
rnumber = 1 # This is the request number. | |
while True: | |
# Randomly select a service to assign a job to. | |
jobs_queue[services_list[randint(0,len(services_list))-1]].append(rnumber) # randint is inclusive | |
rnumber = rnumber + 1 | |
def drive_services(): | |
''' This function runs through the job queues and calls the services''' | |
# Starting one thread for each of the services. The thread talks to just that service. | |
for service in services_list: | |
tr = threading.Thread(target=call_service,args=(service,)) | |
tr.start() | |
def call_service(service): | |
while True: | |
if jobs_queue[service]: | |
fresponse = service.service_routine(jobs_queue[service][0]) # clear out the first in request | |
while fresponse: | |
time.sleep(fresponse) | |
fresponse = service.service_routine(jobs_queue[service][0]) | |
jobs_queue[service].pop(0) # Pop the serviced request. | |
def main_driver(): | |
''' Starts the fetch_data and drive_services threads''' | |
start_services = threading.Thread(target=drive_services) | |
get_input = threading.Thread(target=fetch_data) | |
get_input.start() | |
start_services.start() | |
if __name__ == '__main__': | |
lock = threading.Lock() | |
# More elegant way to do this is to create a class factory | |
sa = ServiceClass("sa",10) | |
sb = ServiceClass("sb",20) | |
sc = ServiceClass("sc",30) | |
services_list = [sa,sb,sc] | |
# This is where the jobs queue up before being pushed into the service routine of the service class | |
## {"a": [2,3,4], "b":[1,7], ...} where 1,2,3.. are the request numbers | |
jobs_queue = {} | |
for servicename in services_list: | |
jobs_queue[servicename] = [] | |
main_driver() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment