Multiple Threads
Explore how Python handles multiple threads and the role of the Global Interpreter Lock in concurrency. Learn to spawn threads for client requests, adjust thread switching intervals for better request handling, and recognize the limitations that GIL imposes on multi-CPU utilization.
We'll cover the following...
We'll cover the following...
Multiple Threads
To mitigate the issues we experienced in the previous section, we'll modify our service to spawn a new thread to deal with each new client request. We can use a ThreadPoolExecutor to handle new client connections but for now, we'll simply spawn threads ourselves. The required changes are:
def run_service(self):
connection = socket.socket()
connection.bind(('localhost', self.server_port))