前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多进程多线程的区别_多进程和多线程效率

多进程多线程的区别_多进程和多线程效率

作者头像
全栈程序员站长
发布2022-09-21 20:51:09
5570
发布2022-09-21 20:51:09
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君

前一篇文章介绍了单任务的HTTP服务器,那么如何实现多任务的呢,本篇文章将实现HTTP服务的并发处理,分别从多进程,多线程,协程的方法来实现,代码有点多,引入了3个文件,重复度有点高,读者只看关键部分,就好了,主要是服务端的数据收发阶段。

测试方法:将html文件方法和代码同路径下,运行代码,在浏览器中输入IP:端口/XX.html即可看到成功解析html文件(这里不讨论html中的内容,只需明白实现思路即可) 如图:

****1. 多进程实现HTTP服务器的并发

代码语言:javascript
复制
import socket
import re
import multiprocessing

""" TCP 的服务端 1,socket 创建socket 2.bind 绑定IP和端口 3.listen 处于监听状态 4.accept 接进来客户端的连接 5.recv/send 接受或者发送信息 6.close 关闭 """


def tcp_creat_socket():
    tcp_ser = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    return tcp_ser


def tcp_bind_listern_info(tcp_socket):
    SER_IP = "127.0.0.1"
    # SER_PORT = int(input("服务器绑定的端口:"))
    SER_PORT = 7080
    SER_INFO = (SER_IP, SER_PORT)
    tcp_socket.bind(SER_INFO)

    LISTEN_NUM = 10
    tcp_socket.listen(LISTEN_NUM)
    return SER_INFO


def tcp_accept(tcp_socket,ser_info):
    # accept 1默认会阻塞,2并返回一个新的套接字用于和接进来的客户端进行数据传输,3并记录客户端的信息,为用户进行服务
    print("服务器的IP:%s,端口:%d,正在等待新的客户端的到来" % (ser_info[0], ser_info[1]))
    tcp_ser_new_socket, client_addr = tcp_socket.accept()
    print("客户端已经到来,信息如下:%s" % str(client_addr))
    return tcp_ser_new_socket


def tcp_recv_message(tcp_socket):
    # recv 返回值为空时,则客户端调用了close
    tcp_ser_recv_info = tcp_socket.recv(1024)
    if "exit" == tcp_ser_recv_info or None == tcp_ser_recv_info:
        print("recv_info error")

    # GET / HTTP/1.1
    # Host: www.baidu.com
    # Connection: keep-alive
    # sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
    # sec-ch-ua-mobile: ?0

    print(">"*120)
    print("服务端接收:%s" % tcp_ser_recv_info.decode("utf-8").splitlines()[0])
    ret = re.match(r"[^/]+(/[^ ]*)", tcp_ser_recv_info.decode("utf-8").splitlines()[0])
    if ret:
        http_page_name = ret.group(1)
        return http_page_name


def tcp_send_message(tcp_socket, page_name):
    # send_info = str(input("输入服务端发送的信息:"))
    if "/" == page_name:
        page_name = "index"  # 默认的页面
    try:
        f = open(f"./{ 
     page_name}.html", "rb")   # 二进制文件
        html_file = f.read()
        f.close()
    except:
        header = "HTTP/1.1 404 NOT FOUND\r\n"  # 相应头部
        respone = "\r\n"  # 空行
        http_headr = header + respone + f"{ 
     page_name} not fount ,please again"
        tcp_socket.send(http_headr.encode("utf-8"))
    else:
        header = "HTTP/1.1 200 OK\r\n"  # 相应头部
        respone = "\r\n"  # 空行
        http_headr = header + respone

        http_body = html_file
        tcp_socket.send(http_headr.encode("utf-8"))
        tcp_socket.send(http_body)

    tcp_socket.close()


def tcp_close(tcp_socket):
    tcp_socket.close()


def service_client(new_socket):
    # recv
    page_name = tcp_recv_message(tcp_socket=new_socket)

    # send
    tcp_send_message(tcp_socket=new_socket, page_name=page_name)


def CC_server():
    # 创建
    tcp_ser =tcp_creat_socket()

    # bind and listen
    ser_info = tcp_bind_listern_info(tcp_socket=tcp_ser)

    while True:  # 循环为多个客户端服务,一直处于监听状态
        new_socket = tcp_accept(tcp_socket=tcp_ser, ser_info=ser_info)

        p_client = multiprocessing.Process(target=service_client,args=(new_socket,))  # 启动一个线程去执行客户端的服务
        p_client.start()
        new_socket.close()

        # close 关闭发送数据的客户端的连接
        tcp_close(tcp_socket=new_socket)

    # close
    tcp_close(tcp_socket=tcp_ser)


if __name__ == "__main__":
    CC_server()
  1. 多线程实现HTTP服务器的并发
代码语言:javascript
复制
import socket
import re
import threading

""" TCP 的服务端 1,socket 创建socket 2.bind 绑定IP和端口 3.listen 处于监听状态 4.accept 接进来客户端的连接 5.recv/send 接受或者发送信息 6.close 关闭 """

def tcp_creat_socket():
    tcp_ser = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    return tcp_ser

def tcp_bind_listern_info(tcp_socket):
    SER_IP = "127.0.0.1"
    # SER_PORT = int(input("服务器绑定的端口:"))
    SER_PORT = 7080
    SER_INFO = (SER_IP, SER_PORT)
    tcp_socket.bind(SER_INFO)

    LISTEN_NUM = 10
    tcp_socket.listen(LISTEN_NUM)
    return SER_INFO

def tcp_accept(tcp_socket,ser_info):
    # accept 1默认会阻塞,2并返回一个新的套接字用于和接进来的客户端进行数据传输,3并记录客户端的信息,为用户进行服务
    print("服务器的IP:%s,端口:%d,正在等待新的客户端的到来" % (ser_info[0], ser_info[1]))
    tcp_ser_new_socket, client_addr = tcp_socket.accept()
    print("客户端已经到来,信息如下:%s" % str(client_addr))
    return tcp_ser_new_socket

def tcp_recv_message(tcp_socket):
    # recv 返回值为空时,则客户端调用了close
    tcp_ser_recv_info = tcp_socket.recv(1024)
    if "exit" == tcp_ser_recv_info or None == tcp_ser_recv_info:
        print("recv_info error")

    # GET / HTTP/1.1
    # Host: www.baidu.com
    # Connection: keep-alive
    # sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
    # sec-ch-ua-mobile: ?0

    print(">"*120)
    print("服务端接收:%s" % tcp_ser_recv_info.decode("utf-8").splitlines()[0])
    ret = re.match(r"[^/]+(/[^ ]*)", tcp_ser_recv_info.decode("utf-8").splitlines()[0])
    if ret:
        http_page_name = ret.group(1)
        return http_page_name

def tcp_send_message(tcp_socket, page_name):
    # send_info = str(input("输入服务端发送的信息:"))
    if "/" == page_name:
        page_name = "index"  # 默认的页面
    try:
        f = open(f"./{ 
     page_name}.html", "rb")   # 二进制文件
        html_file = f.read()
        f.close()
    except:
        header = "HTTP/1.1 404 NOT FOUND\r\n"  # 相应头部
        respone = "\r\n"  # 空行
        http_headr = header + respone + f"{ 
     page_name} not fount ,please again"
        tcp_socket.send(http_headr.encode("utf-8"))
    else:
        header = "HTTP/1.1 200 OK\r\n"  # 相应头部
        respone = "\r\n"  # 空行
        http_headr = header + respone

        http_body = html_file
        tcp_socket.send(http_headr.encode("utf-8"))
        tcp_socket.send(http_body)

    # tcp_socket.close()

def tcp_close(tcp_socket):
    tcp_socket.close()

def service_client(new_socket):
    # recv
    page_name = tcp_recv_message(tcp_socket=new_socket)

    # send
    tcp_send_message(tcp_socket=new_socket, page_name=page_name)



def CC_server():
    # 创建
    tcp_ser = tcp_creat_socket()

    # bind and listen
    ser_info = tcp_bind_listern_info(tcp_socket=tcp_ser)

    while True:  # 循环为多个客户端服务,一直处于监听状态
        new_socket = tcp_accept(tcp_socket=tcp_ser, ser_info=ser_info)

        th_client = threading.Thread(target=service_client,args=(new_socket,))  # 启动一个线程去执行客户端的服务,关闭在线程里面实现
        th_client.start()


    # close
    tcp_close(tcp_socket=tcp_ser)


if __name__ == "__main__":
    CC_server()

3.协程实现HTTP服务器的并发**

代码语言:javascript
复制
import socket
import re
import gevent
from gevent import monkey

""" TCP 的服务端 1,socket 创建socket 2.bind 绑定IP和端口 3.listen 处于监听状态 4.accept 接进来客户端的连接 5.recv/send 接受或者发送信息 6.close 关闭 """
monkey.patch_all()


def tcp_creat_socket():
    tcp_ser = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    return tcp_ser


def tcp_bind_listern_info(tcp_socket):
    SER_IP = "127.0.0.1"
    # SER_PORT = int(input("服务器绑定的端口:"))
    SER_PORT = 7080
    SER_INFO = (SER_IP, SER_PORT)
    tcp_socket.bind(SER_INFO)

    LISTEN_NUM = 10
    tcp_socket.listen(LISTEN_NUM)
    return SER_INFO


def tcp_accept(tcp_socket,ser_info):
    # accept 1默认会阻塞,2并返回一个新的套接字用于和接进来的客户端进行数据传输,3并记录客户端的信息,为用户进行服务
    print("服务器的IP:%s,端口:%d,正在等待新的客户端的到来" % (ser_info[0], ser_info[1]))
    tcp_ser_new_socket, client_addr = tcp_socket.accept()
    print("客户端已经到来,信息如下:%s" % str(client_addr))
    return tcp_ser_new_socket


def tcp_recv_message(tcp_socket):
    # recv 返回值为空时,则客户端调用了close
    tcp_ser_recv_info = tcp_socket.recv(1024)
    if "exit" == tcp_ser_recv_info or None == tcp_ser_recv_info:
        print("recv_info error")

    # GET / HTTP/1.1
    # Host: www.baidu.com
    # Connection: keep-alive
    # sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
    # sec-ch-ua-mobile: ?0

    print(">"*120)
    print("服务端接收:%s" % tcp_ser_recv_info.decode("utf-8").splitlines()[0])
    ret = re.match(r"[^/]+(/[^ ]*)", tcp_ser_recv_info.decode("utf-8").splitlines()[0])
    if ret:
        http_page_name = ret.group(1)
        return http_page_name


def tcp_send_message(tcp_socket, page_name):
    # send_info = str(input("输入服务端发送的信息:"))
    if "/" == page_name:
        page_name = "index"  # 默认的页面
    try:
        f = open(f"./{ 
     page_name}.html", "rb")   # 二进制文件
        html_file = f.read()
        f.close()
    except:
        header = "HTTP/1.1 404 NOT FOUND\r\n"  # 相应头部
        respone = "\r\n"  # 空行
        http_headr = header + respone + f"{ 
     page_name} not fount ,please again"
        tcp_socket.send(http_headr.encode("utf-8"))
    else:
        header = "HTTP/1.1 200 OK\r\n"  # 相应头部
        respone = "\r\n"  # 空行
        http_headr = header + respone

        http_body = html_file
        tcp_socket.send(http_headr.encode("utf-8"))
        tcp_socket.send(http_body)

def tcp_close(tcp_socket):
    tcp_socket.close()


def service_client(new_socket):
    # recv
    page_name = tcp_recv_message(tcp_socket=new_socket)

    # send
    tcp_send_message(tcp_socket=new_socket, page_name=page_name)


def CC_server():
    # 创建
    tcp_ser = tcp_creat_socket()

    # bind and listen
    ser_info = tcp_bind_listern_info(tcp_socket=tcp_ser)

    while True:  # 循环为多个客户端服务,一直处于监听状态
        new_socket = tcp_accept(tcp_socket=tcp_ser, ser_info=ser_info)
        gevent.spawn(service_client, new_socket)   # 直接进行传参即可

    # close
    tcp_close(tcp_socket=tcp_ser)


if __name__ == "__main__":
    CC_server()

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168450.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档