多线程

本文详细探讨了多线程的概念,包括线程的创建、同步与通信、死锁问题以及线程池的使用。通过实例分析,阐述了如何在实际开发中有效利用多线程提高程序运行效率,并讨论了多线程环境下可能遇到的挑战和解决方案。
摘要由CSDN通过智能技术生成
# coding:utf-8
import socket
import threading
import time

EOL1 = '\n\n'
EOL2 = '\n\r\n'
body = '''Hello, world! <h1> from  earth </h1> - from {thread_name}'''
response_params = [
    'HTTP/1.0 200 OK',
    'Date: Sat, 10 jun 2019 01:01:01 GMT',
    'Content-Type: text/plain; charset=utf-8',
    'Content-Length: {length}\r\n',
    body,
]
response = b'\r\n'.join(response_params)

def handle_connection(conn, addr):
    request = ""
    while EOL1 not in request and EOL2 not in request:
        request += conn.recv(1024)
    # print(request)
    current_thread = threading.currentThread()
    content_length = len(body.format(thread_name=current_thread.name))
    print(current_thread.name, '-------', 'sleep 10', int(time.time()))
    time.sleep(10)
    conn.send(response.format(thread_name=current_thread.name, len
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值