python中socket+threading的自定義併發

One of them發表於2018-08-23

socket+threading自定義執行緒池實現併發

"""
   user-defined threading-pool Concurrency-server.
   不儲存cnn, 扔進去,讀取執行,結束.

   總結:
   執行緒結束了就結束了. 所以如何實現執行緒不結束得去處理任務才是問題.

   流程如下.
   建立queue來put socket任務和get socket任務,
   建立threading-pool讓其中執行緒能迴圈get socket任務.
   so 迴圈 apply_async不停put socket任務就完了.
   如果主程式末尾加上join則會等待每個傳入的socket任務處理結束, 使得不能實現多client訪問, 所以不使用join來等待任務結束.
   大概是這樣.
"""
import threading
import queue
import socket


class DeThread:
    def __init__(self, server_queue, server_number):
        self.queue = server_queue
        for i in range(1, server_number + 1):
            de_thread = threading.Thread(target=self.work, name="{}".format(i), daemon=True)
            de_thread.start()

    def work(self):
        print("[+] Customer service {} online.".format(threading.current_thread().name))
        while True:
            try:
                client_mission, client_address = self.queue.get()
                client_mission.send(b"welcome to server.")
                print("[+] {} is connected.".format(client_address))
                while True:
                    client_data = client_mission.recv(1024)
                    if not client_data == b"quit":
                        print("[+] {} talk: {}".format(client_address, client_data))
                        client_mission.send(b"Received of information.")
                    else:
                        client_mission.send(b"Goodbye.")
                        print("[+] {} is close".format(client_address))
                        client_mission.close()
                        break
            except Exception as error_work:
                print("[-] Error Work: {}".format(error_work))
            finally:
                self.queue.task_done()

    def apply_async(self, client_connect):
        self.queue.put(client_connect)

    def join(self):
        self.queue.join()


def func(server):
    connect, address = server.accept()
    # clientList.append((connect, address))
    connects = (connect, address)
    return connects


if __name__ == '__main__':
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((socket.gethostbyname(socket.gethostname()), 4444))
    # server.bind(("0.0.0.0", 4444))
    server.listen(5)

    # clientList = []
    serverQueue = queue.Queue()
    sPool = DeThread(serverQueue, 2)    # 生成執行緒數量.
    print("[+] Start listen......")
    while True:
        try:
            sPool.apply_async(func(server))     # 放入任務.
        except Exception as error_pool:
            print("[-] Error Pool:{}".format(error_pool))
    # finally:
    #  sPool.join()

相關文章