Python Network Programming 16 - Multithreading ( Socket Programming )

preview_player
Показать описание

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.

---

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.

Next Video - Running the Multi-Client Program Live

#python
Рекомендации по теме
Комментарии
Автор

Hi Attreya, Can please once explain me the order of execution of functions create_workers(), work(), create_jobs(), because I could not understand
create_workers()
create_jobs()
this sequence of execution in the last of the program.

HimanshuMishra-fpvn
Автор

bro can you resolve address already in use problem ... i have to change the port number many times, ,,

Bhanucsgo
Автор

create_socket() and bind_socket() can be written outside. It doesn't need to be inside infinite loop of work()

shiwang
Автор

Can you zoom a little bit in the future. Your content is already awesome.

adityachitrigemath
Автор

Hello, I need to implement multiple servers and multiple clients. And I need to do a dynamic discovery of hosts with multicast. But I dont know how, could help pls sir?

HankeyMassacre
Автор

hello thanks for the work done but when i type in list no list are displayed

katohassankigozi
Автор

After all, I type list. But It doesn’t show any output. No error also 😢. 🤷‍♂️

sheshanhebron
Автор

it doesn't show the list of connections when i typing list or any other commands

TechWithUmraz
Автор

it doesn't show the list of connections when typing typing list or any other commands

mohammadansari
Автор

Your code is slightly wrong (even on your GitHub).
In line-109 there must be one more level of indentation.

What was the need of using infinite while loop in work() ??

Please can you explain, your order of calling function in last lines of your code...
why is create_workers() called first/before than create_jobs() ?
As create_ workers needs work(), & work needs create_jobs() so... create_jobs() must be called before than create_workers()!!!

rishi
Автор

When i send the request list it just constanly prints out the connections it dose not stop just a constant loop any sulotions ?

STONEDSOLDIER
Автор

I need to connect 3 clients to my server from one computer only(virtual environment), so how should I create the thread?
One thread for each client or one thread which can create as many client as needed?

shivangigupta
Автор

Without sharing client file to someone can I do this? And how to I do

SonuRaj-erhn
Автор

Actually I am facing problem with 'address already in use' once after I exit the connection.
can you solve this issue

nishantthakur
Автор

s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

It shows me the same error even when the firewall is turned off and the client is run on two different machines, so can you please comment on that pls

saran-ravi
Автор

cd command does not work after client selecting

mohammedmansour
Автор

How to modified code for network relay operation

girdharikhoth
Автор

Why my ip is static and not dynamic?? Is there any specific

gauravbisht
Автор

Hey why did u create the workers first then the jobs

safireblazeplays
welcome to shbcf.ru