Python Forum

Full Version: Client server Multithreading
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have been working on creating a simple 3-client 1-server model to check and update the user-supplied text file. The server is doing this by comparing it with his own list of words.

All clients have been successfully connected and have got the updated file.

Now further I wanted to allow the server to keep on polling the clients after some 60 seconds to check if there are any new words in their queue and retrieve if any.

Could someone help as to what functionality the server needs to use to do this? I have been trying to use select but could not really properly make use of it. I know there should be some locking mechanism as well so that it does not affect the multi-threaded environment. But how exactly it can be accomplished?

NOTE: I am using python 3.7
Firstly, I assume you're using sockets since I did not see any mention of a specific library. I would create a Server class and within it two main functions. The first receives and handles signals from the clients. The second one, which could be run within a thread, will every 60 seconds, ping each client to send information of the current status of their words, which the server can compare with it's own. Below is a little example of my idea (don't know if I made any typos in the code, but it's just for a general idea).

import sockets
import select
import threading
import time


class Server:
    def __init__(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.bind((IP, PORT))

        self.sockets = [self.socket]
        self.clients = []
        self.listening = False

        self.codes = {
            0: self.compare_with_client,
        }

    def run_recv(self):
        self.socket.listen()
        self.listening = True
        while self.listening:
            ready_socks, _, error_socks = select.select(self.sockets, [], self.sockets)
            for sock in ready_socks:
                if sock == self.socket:
                    client, address = self.s.accept()
                    self.socks.append(client)
                    self.clients.append(client)
                else:
                    # Receive info from socket
                    # Figure out code (meaning of message)
                    # Execute function based on code given by client
                    self.codes[code](message_data)


    def run_ping_client(self):
        while self.listening:
            time.sleep(60)
            if not self.listening:
                return
            for client in self.clients:
                # Ping client with code 0
                # Client will ping back under the same code

    def run(self):
        thread = threading.Thread(target=self.run_ping_client)
        thread.start()
        self.run_recv()

    def compare_with_client(self, data):
        # Do whatever with the data from the client
Thank you so much for your help. I'll try to implement this idea in my code.
And yes, I am using sockets to connect my clients with the server.
I was thinking to store the elements in the client queue. THen convert the queue to a list and send it over to the socket.

Then the server will send a message after some time asking for some info. If the client has some words with it, it will respond.

Timer I am still confused, since:- If we use time.sleep(60), it just hangs the complete program.

Will it not affect the functionality?



Also, 1 more query... Can I make use of JSON to convert the entire list to bytes. if yes, then how can I send the JSON converted bytes over the socket?
send.encode() command does not work for it. It just gives the socket timeout error.
run_ping_client is a thread, so time.sleep will only hold up the code running within that thread. Also yes, you can use json to easily send lists over the socket. json.dumps will take in a list and return a string which you can encode and send across the socket. json.loads will take in a string and turn it back into a list.
Hi There,
Is Socket library supports python 3.9 or vice-a-versa?
I have installed python 3.9 and while installing socket it giving error "No matching distribution found".
(Apr-20-2021, 07:20 AM)sankarachari Wrote: [ -> ]Hi There,
Is Socket library supports python 3.9 or vice-a-versa?
I have installed python 3.9 and while installing socket it giving error "No matching distribution found".

Yes, it is supported.. did you do import socket before using any socket functionality?
The socket library should come with Python download iirc, so you shouldn't have to use pip to install it. Also, you should make a new thread next time rather than using one on a separate question.