日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python tcp server分包_如何创建线程池来监听tcpserver包python

發布時間:2023/11/27 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python tcp server分包_如何创建线程池来监听tcpserver包python 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我試圖創建線程池來同時對傳入的tcp包執行一些操作。在

我在python3中找不到任何內置線程池。我也讀了一些關于multiprocessing.Pool的文章,但是它不支持內存共享。所以我使用Queue來模擬線程池。在

為每個線程創建一個隊列是否更好?

有沒有人有建議或更好的解決方案來改進以下自定義線程池代碼?在import socketserver

import threading

from queue import Queue

class ThreadPool:

tcp_queue = Queue()

# redis = redis connection

def __init__(self, thread_worker_numbers=10):

for i in range(thread_worker_numbers):

threading.Thread(target=self.worker).start()

def worker(self):

while True:

packet = self.tcp_queue.get()

print('thread: %s received packet: %s' % (threading.current_thread().getName(), packet))

# TODO do process on packet

def queue(self, packet):

self.tcp_queue.put(packet)

def queue_count(self):

return self.tcp_queue.qsize()

class TCPServerHandler(socketserver.BaseRequestHandler):

thread_pool = ThreadPool()

def handle(self):

# self.request is the TCP socket connected to the client

self.data = self.request.recv(1024).strip()

packet = self.data.decode("utf-8")

print(packet)

self.thread_pool.queue(packet)

# print("%s wrote: %s" % (self.client_address[0], packet))

# just send back the same data, but upper-cased

self.request.sendall(bytes('{ok: 1, msg:""}', 'utf-8'))

if __name__ == "__main__":

HOST, PORT = "0.0.0.0", 9999

# Create the server, binding to localhost on port 9999

server = socketserver.TCPServer((HOST, PORT), TCPServerHandler)

# Activate the server; this will keep running until you

# interrupt the program with Ctrl-C

server.serve_forever()

總結

以上是生活随笔為你收集整理的python tcp server分包_如何创建线程池来监听tcpserver包python的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:python tcp server分包_如何创建线程池来监听