成都网站建设设计

将想法与焦点和您一起共享

创新互联Python教程:python线程阻塞的解决

问题分析

1、当出现多个客户端请求时,由于单个线程会堵塞,需要多线程处理多个客户端请求。

解决方法

2、每个客户端的请求都会生成子线程,然后处理请求

from threading import Thread
 
while True:
        client_sock, address = sock.accept()
        thread = Thread(target=echo_handler, args=(client_sock, address))
        thread.start()

但是有一个问题:当突然大量请求连接,消耗系统资源达到上限时,程序很可能无法处理后续请求。

以上就是python线程阻塞的解决方法,希望对大家有所帮助。更多Python学习指路:创新互联Python教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。


分享文章:创新互联Python教程:python线程阻塞的解决
浏览地址:http://chengdu.cdxwcx.cn/article/djegedj.html