11import os
22from queue import Queue
33from threading import Thread
4-
4+ import threading
55from http .status import *
6-
6+ import time
77from cache .cache import *
88from http .http_response import HTTPResponse
99from selector import sel
1010from utils .event_loop_app_exception import EventLoopAppException
11+ import socket
1112
12- NUM_OF_THREADS = 4
13+ NUM_OF_THREADS = 1
1314
1415class EventLoop :
1516
@@ -21,7 +22,6 @@ def __init__(self, event_queue, cache_policy=LRU):
2122 for i in range (NUM_OF_THREADS ):
2223 t = Thread (target = self .read )
2324 t .start ()
24- print ("Thread " + str (i ) + " started!" )
2525
2626 def start (self ):
2727 while True :
@@ -35,7 +35,6 @@ def execute(self):
3535 if event .is_disk_io ():
3636 # Check whether the requested data is cached.
3737 cached_response_bytes = self .cache .get (event .request_uri )
38-
3938 if cached_response_bytes != - 1 :
4039 event .response_bytes = cached_response_bytes
4140 event .disk_io = False
@@ -49,44 +48,47 @@ def execute(self):
4948
5049 @staticmethod
5150 def close_or_keep_alive (event ):
52- print ("Event_connection_info:" + str (event .connection ))
51+ # print("Event_connection_info:" + str(event.connection))
5352 if event .connection == 'keep-alive' :
54- print ("Event Connnection is keep alive!" )
53+ # print("Event Connnection is keep alive!")
5554 pass
5655 else :
5756 sel .unregister (event .CLIENT_SOCKET )
5857 event .CLIENT_SOCKET .close ()
59- print ("Connection from client is closed." )
58+ # print("Connection from client is closed.")
6059
6160 def send_event (self , event ):
62- print ("Event sending started!" )
61+ # print("Event sending started!")
62+ # begin = time.time()
6363 bytes_to_send = HTTPResponse .respond (HTTP_200_OK , event )
6464 event .CLIENT_SOCKET .setblocking (True )
6565 event .CLIENT_SOCKET .sendall (bytes_to_send )
66- print ("Event Sent!" )
66+ # end = time.time()
67+ # print('Send elapsed time: ' + str(end - begin))
68+ # print("Event Sent!")
6769
6870 def read (self ):
6971 while True :
7072 self .read_aux ()
7173
7274 def read_aux (self ):
7375 event = self .disk_io_queue .get (block = True , timeout = None )
74- print ("after getting event" )
7576 event = self .process_disk_io (event )
7677# self.disk_io_queue.task_done()
77- self .event_queue .enqueue (event )
78+ self .send_event (event )
79+ # self.event_queue.enqueue(event)
7880
7981 def process_disk_io (self , event ):
80- print ( "Read file from disk." )
82+ # begin = time.time( )
8183 try :
8284 with open (os .path .dirname (__file__ ) + '/resources' + event .request_uri , 'rb' ) as f :
8385 event .response_bytes = f .read ()
8486 # Newly read file is inserted into cache.
8587 self .cache .set (event .request_uri , event .response_bytes )
8688 except :
8789 raise EventLoopAppException (HTTP_404_NOT_FOUND , 'File does not exist. Cannot process event' , event )
88-
90+ # end = time.time()
91+ # print('Disk I/O elapsed time: ' + str(end-begin))
8992 event .disk_io = False
90- print ("Done reading file from disk." )
9193 return event
9294
0 commit comments