Python Forum
How to get my Python SocketServer work parallely?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get my Python SocketServer work parallely?
#1
Hi

I have made a multithreaded Python socket server.

It supports multiple socket client connections. However, it serves the client only synchronously. If one client is connected to it, then the other client needs to wait until the current clients data sending/receiving to/from socket server is done.

My code is:

import socket
from _thread import *

def multi_threaded_client(connection):
    response = ''
    while True:
        data = connection.recv(10000000)
        response += data.decode('utf-8')
        if not data:
            print(response)
            break
        connection.send(bytes(some_function_name(response), "utf-8"))
    connection.close()



class socketserver:
    def __init__(self, address='', port=9090):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.address = address
        self.port = port
        self.sock.bind((self.address, self.port))
        self.cummdata = ''

    def recvmsg(self):
        self.sock.listen(65535)
        self.conn, self.addr = self.sock.accept()
        print('connected to', self.addr)
        self.cummdata = ''
        
        start_new_thread(multi_threaded_client, (self.conn, ))

        return self.cummdata
        
    def __del__(self):
        self.sock.close()

serv = socketserver('127.0.0.1', 9090)

print('Socket Created at {}. Waiting for client..'.format(serv.sock.getsockname()))

while True:
    msg = serv.recvmsg()
1) I would like it to work parallely, but I am stuck here, since I am quite new to all these topics

May be, the Socket server should run multiple instances of itself for each new connection made by a client. (Just guessing!)

2) Also,
initially the socket server has 0 clients connected to it.
I want that when some clients are connected to it & if all clients disconnect from it, then it should get closed automatically.
I am unable to figure out these.

Kindly help
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Set request timeout for socketserver voltron 0 4,423 May-24-2018, 06:17 AM
Last Post: voltron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020