并发编程

Posted plf-jack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了并发编程相关的知识,希望对你有一定的参考价值。

并发编程

可以显示多个客户端,同时连接服务端。客户端能同时发送消息给服务器

客户端-1
import  socket
import struct
import time
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

addr = ('127.0.0.1',8000)

client.connect(addr)

while True:
    # 发送消息
    # msg = input("请输入你要讲的话>>>>")
    time.sleep(2)
    msg = 'dir'
    print(msg)
    client.send(bytes(msg.encode('utf8')))

    leng = client.recv(4)
    date_lens = struct.unpack('i', leng)[0]
    # 接受消息
    leng = client.recv(date_lens)
    print(leng.decode('gbk'))
客户端-2
import  socket
import struct
import time
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

addr = ('127.0.0.1',8000)

client.connect(addr)

while True:
    # 发送消息
    # msg = input("请输入你要讲的话>>>>")
    time.sleep(2)
    msg = 'dir'
    print(msg)
    client.send(bytes(msg.encode('utf8')))

    leng = client.recv(4)
    date_lens = struct.unpack('i', leng)[0]
    # 接受消息
    leng = client.recv(date_lens)
    print(leng.decode('gbk'))
客户端-3
import  socket
import struct
import time
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

addr = ('127.0.0.1',8000)

client.connect(addr)

while True:
    # 发送消息
    # msg = input("请输入你要讲的话>>>>")
    time.sleep(2)
    msg = 'dir'
    print(msg)
    client.send(bytes(msg.encode('utf8')))

    leng = client.recv(4)
    date_lens = struct.unpack('i', leng)[0]
    # 接受消息
    leng = client.recv(date_lens)
    print(leng.decode('gbk'))
客户端-4
import  socket
import struct
import time
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

addr = ('127.0.0.1',8000)

client.connect(addr)

while True:
    # 发送消息
    # msg = input("请输入你要讲的话>>>>")
    time.sleep(2)
    msg = 'dir'
    print(msg)
    client.send(bytes(msg.encode('utf8')))

    leng = client.recv(4)
    date_lens = struct.unpack('i', leng)[0]
    # 接受消息
    leng = client.recv(date_lens)
    print(leng.decode('gbk'))
服务端
import  socketserver
import subprocess
import struct

class MyHandler(socketserver.BaseRequestHandler):   # 重写方法
    # 通信循环
    def handle(self):
        while True:
            # 接受消息
            client_msg = self.request.recv(1024)
            common = client_msg.decode('utf8')
            print(common)
            obj = subprocess.Popen(common,
                                   shell=True,
                                   stderr=subprocess.PIPE,
                                   stdout=subprocess.PIPE
                                   )
            stdout = obj.stdout.read()  # 拿到正常输出的信息
            stderr = obj.stderr.read()  # 拿到错误输出的信息

            msg_len = len(stderr) + len(stdout) # 计算传输数据的长度
            one = struct.pack('i', msg_len) #  转化
            print(one)
            self.request.send(one)
            self.request.send(stdout + stderr)


if __name__ == '__main__':
    addr = ('127.0.0.1',8000)
    service = socketserver.ThreadingTCPServer(addr,MyHandler,bind_and_activate=True)
    service.serve_forever()

当你运行服务端后,依次运行对应客户端文件,你会发现此时就是并发在处理数据。

以上是关于并发编程的主要内容,如果未能解决你的问题,请参考以下文章

Go语言学习之旅--并发编程

并发编程路线

java并发编程看啥书比较好

JAVA并发编程:并发编程的认识

并发编程的基础

Java并发编程之美