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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

socket 多线程安全、粘包问题

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 socket 多线程安全、粘包问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

腳本如下:

# -*- coding:utf-8 -*- ''' @Author: Stefan@File: server_listener.py@Date: 2016-11-09If you execute this test script on servers, you have to modify the system parameter below: sysctl net.ipv4.tcp_tw_recycle=1 ''' import sys sys.path.append('/export/servers/app/xxxx') import time import socket import threading import multiprocessing from src.xxxxclient.xxxx_client.lib.socket import header_pack, receive HEADER_LENGTH = 16 DEFAULT_TCP_PORT = 1104 DATA = {'xxxx_ADMIN': 'thread_keepalive'}count_of_processes = range(1) # DO NOT modify the parameter below count_of_threads = range(1)# Unit: second execute_time = 60def loop(times):""":return:"""time_start_now = time.time()time_future = time_start_now + execute_timewhile True:if time_start_now > time_future:breakelse:time_start = time.time()S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# S.connect(('127.0.0.1', DEFAULT_TCP_PORT))S.connect(('x.x.x.x', DEFAULT_TCP_PORT))header, data = header_pack(DATA, HEADER_LENGTH)S.sendall(header+data)header, result = receive(S, HEADER_LENGTH, 1024)S.close()time_end = time.time()time_used = time_end - time_startprint "Thread lasted %.2f sec. Status: %s" % (time_used, result)time_end_now = time.time()time_gap = time_end_now - time_start_nowtime_start_now += time_gapdef multi_threads():""":return:"""threads_list = list()for i in count_of_threads:t = threading.Thread(target=loop, args=str(count_of_threads[i]).split())threads_list.append(t)for i in count_of_threads:threads_list[i].start()for i in count_of_threads:threads_list[i].join()class muliti_process(multiprocessing.Process):""""""def __init__(self):""":return:"""multiprocessing.Process.__init__(self)self.processes_list = list()def run(self):""":return:"""for i in count_of_processes:p = multiprocessing.Process(target=multi_threads,)self.processes_list.append(p)for i in self.processes_list:i.start()if __name__ == '__main__':p = muliti_process()global_start = time.time()p.start()p.join()global_end = time.time()global_used = global_end - global_startprint "==================\nTotal %.2f sec" % global_used

轉載于:https://www.cnblogs.com/stefan-liu/p/6138565.html

總結

以上是生活随笔為你收集整理的socket 多线程安全、粘包问题的全部內容,希望文章能夠幫你解決所遇到的問題。

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