Python Network Programming 16 Multithreading Socket Programming
>> YOUR LINK HERE: ___ http://youtube.com/watch?v=O_Y1NLLP0d8
We have finished most of the code of the server.py file and finally we can set up our threading. Now if you remember we want to split this program into two tasks. The first thread is going to be waiting, and listening for connections from other clients and when it get a connection request, it's going to accept that connection and add it the connections list. The second thread is going to come into action when we want to send some kind of commands to a specific client. • So for people who are unfamiliar with threading in Python, I am going to give you a quick overview of what threading is. When you want to do tasks parallel or simultaneously then you have to use the concept of threads. • Imagine there is a factory in which a lot people are working. These people are called workers. Each worker is doing a specific job. But all of the workers are working simultaneously to create a product. Similarly in our program you can think of threads as workers. We have 2 workers in our program who will have to work simultaneously. The first worker is going to handle connections and the second one is going to send commands. • ---------------------------------- • Now let's discuss how we are going to be coding threads into our program. We will create 3 functions in our program. • The first function is actually going to create the worker threads. We will use a 'for' loop to create both the threads quickly. Inside the 'for' loop we will create the threads using the threading.thread function and store it in a variable 't'. After this we will set the daemon of the threads as equal to True. What this means is that we are telling the threads to release the memory after the program ends. If we don't set it equal to true then the threads are going to run in background even though the program has ended. After this we will simple start the thread using the start function. • --- • Now The second function is going to store jobs in QUEUE. If you remember at the starting we created a variable called job number and it is storing two elements in a list. We need to copy both of these elements to the queue. We are doing this because threads look for jobs in a queue and not in lists. The list format was only there so you could understand the concept of threads better. • --- • The third function called the work function will distribute work according to the job number. If the job number in queue is 1 then it's going to make the thread handle connections and if it's 2 then send commands. • So these are the 3 functions that we will be creating in our code. Now that we are going to write the code things are going to get much clearer. • Github Code - https://github.com/attreyabhatt/Rever... • Next Video - Running the Multi-Client Program Live • • Python Network Programming 17 - Runni... • Python Network/Socket Programming Playlist - • Python Network Programming - TCP/IP S... • Subscribe - / @buildwithpython • Website - www.buildwithpython.com • Instagram - / buildwithpython • #python
#############################
