Close socket for listening in python stream

I have a problem trying to find out about sockets for network communications. I made a simple thread that listens for connections and creates processes for connecting clients. My problem is that I cannot get the thread to connect properly, as I have not found a way to cancel socket.accept () - call when I want to exit the program.

My code is as follows:

class ServerThread( threading.Thread ): def __init__(self, queue, host, port): threading.Thread.__init__(self) self.queue = queue self.running = True self.hostname = host self.port = port def run(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.bind((self.hostname, self.port)) self.socket.listen(1) while self.running: try: conn, address = self.socket.accept() process = Process(target=server_slave, args=(conn, address, self.queue)) process.daemon = True process.start() except socket.timeout: pass def stop(self): self.running = False self.socket.close() 

I managed to close the program by setting self.setDaemon(True) and just exit the main program, passing everything to a large garbage collector, but this seems like a bad solution. I also tried setting a timeout for the socket, but this results in a [Errno 35] Resource temporarily unavailable (regardless of the actual timeout, even when I set it in years ...).

What am I doing wrong? Did I develop a thread at a dead end or miss something about accepting connections?

+6
source share
3 answers

One way to force the thread to close seems to be making a connection to the socket, thereby continuing to terminate the thread.

 def stop(self): self.running = False socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect( (self.hostname, self.port)) self.socket.close() 

This works, but it still seems like this might not be optimal ...

+8
source

In most cases, you will open a new thread or process as soon as the connection is accepted. To close the connection, break the while loop. Garbage collection will remove the thread or process, but combining will ensure that no one is left behind.

Persistent sockets close when the user closes them or time out. Unstable, like static web pages, will close after sending the information.

Here is a good example of a persistent socket server in Python. It uses multiprocessing, which means that it can work across multiple cores for CPU related tasks. Better known as multithreading.

 import socket import multiprocessing def run(): host = '000.000.000.000' port = 1212 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', port)) sock.listen(3) while True: p = multiprocessing.Process(target=worker, args=sock.accept()).start() def worker(conn, addr): while True: if data == '': #remote connection closed break if len(dataList) > 2: # do stuff print 'This code is untested' run() 
0
source

Partially Tested Solution

  • Put self.socket.settimeout(0.1) right before while
  • Put conn.settimeout(None) right after accept
-1
source

Source: https://habr.com/ru/post/945775/


All Articles