线程数据安全_笔记

一、多线程服务器和客户端

from socket import *
from threading import *
import os
from json import *

class DealClientThread(Thread):
    def __init__(self, connection: socket, address):
        super().__init__()
        self.connection = connection
        self.address = address

    def run(self) -> None:
        while True:
            re_data = self.connection.recv(1024)
            message = re_data.decode('utf-8')
            if message == 'text':
                self.connection.send('我是文字信息!'.encode())
            elif message == 'image':
                all_files = os.listdir('./images')
                # 发送所有的图片名字
                self.connection.send(dumps(all_files).encode())
                # 接收图片名
                image_name = self.connection.recv(1024)
                # 判断图片是否存在
                image_path = './images/'+image_name.decode(encoding='utf-8')

                if os.path.exists(image_path):
                    print('文件存在!')
                    self.connection.send('exists'.encode())
                    with open(image_path, 'rb') as f:
                        self.connection.send(f.read())
                else:
                    print('文件不存在!')
                    self.connection.send('notExists'.encode())

            else:
                self.connection.close()
                break


def creatServer():
    server = socket()
    server.bind(('10.7.181.109', 9997))
    server.listen(512)

    while True:
        print('开始监听....')
        connection, adderss = server.accept()
        t = DealClientThread(connection, adderss)
        t.start()


if __name__ == '__main__':
    creatServer()

# ==================================================


from socket import socket
from json import *

client = socket()
client.connect(('10.7.181.109', 9997))

while True:
    print('1.获取文字信息\n2.获取图片信息\n3.退出')
    value = input('请选择(1-3):')
    if value == '1':
        client.send('text'.encode())
        # 接收服务器返回的文字信息
        message_data = client.recv(1024)
        print(message_data.decode(encoding='utf-8'))
    elif value == '2':
        client.send('image'.encode())
        # 接收服务返回的所有的图片信息
        image_message = client.recv(1024)
        all_files = loads(image_message.decode(encoding='utf-8'))
        for index in range(len(all_files)):
            print('%d: %s' % (index, all_files[index]))
        # 选择需要的图片
        num = int(input('请选择需要的图片名(0-%d):' % len(all_files)))
        # 发送图片名
        client.send(all_files[num].encode())
        # 接收图片
        is_exsits = (client.recv(1024)).decode(encoding='utf-8')
        if is_exsits == 'exists':
            image_data = bytes()    # 创建空的二进制
            while True:
                small_image_data = client.recv(1024)
                image_data += small_image_data
                if len(small_image_data) < 1024:
                    break
            with open('./client/'+all_files[num], 'wb') as f:
                f.write(image_data)
                print('图片下载完成!')
        else:
            print('404,图片不存在!')

    else:
        client.send('exit'.encode())
        client.close()
        break

总结:

——数据的存储跟线程无关,一个进程中的数据在多个线程中可以直接使用

情况一: 在子线程中使用主线程中的数据
from time import sleep
from threading import Thread, Lock
list1 = [1, 2, 3]
def func1():
    list1.append(100)
def func2():
    list1[0] = 'hello'
t1 = Thread(target=func1)
t2 = Thread(target=func2)
t1.start()
t2.start()
t1.join()
t2.join()
print(list1)    # ['hello', 2, 3, 100]

情况2: 在子线程中使用子线程中产生的数据

def func1():
    global list1
    list1 = [1, 2, 3]
def func2():
    t = Thread(target=func1)
    t.start()
    print(list1)
t1 = Thread(target=func2)
t1.start()

获取锁对象
获取数据
数操作完成后
释放锁对象

注意: 使用锁的时候保证一个数据对应一把锁

from threading import Thread, Lock
class Account:
    """银行账号类"""
    def __init__(self, name, tel, balance, bank='招商银行'):
        self.bank = bank
        self.card_number = '6233392838382383'
        self.name = name
        self.tel = tel
        self.balance = balance
        self.lock = Lock()    # 1.创建锁(保证一个数据一把锁)

    def save_money(self, amount):
        print('=====开始存钱!======')
        # 2.使用锁
        self.lock.acquire()
        # 获取余额
        bl = self.balance
        # print('存钱余额1:',bl)
        sleep(2)
        self.balance = bl + amount
        # 3.释放锁
        self.lock.release()
        # print('存钱余额2:', self.balance)
        print('=====存钱结束!======')


    def draw_money(self, amount):
        print('=====开始取钱!======')
        self.lock.acquire()
        bl = self.balance
        # print('取钱余额1:', bl)
        if bl < amount:
            print('余额不足!')
            print('=====取钱结束======')
            return
        sleep(3)
        self.balance = bl - amount
        self.lock.release()
        # print('取钱余额2:', self.balance)
        print('=====取钱结束======')


account = Account('余婷', '153000782', 10000)

t1 = Thread(target=account.save_money, args=(20000,))
t2 = Thread(target=account.draw_money, args=(5000,))
t1.start()
t2.start()
t1.join()
t2.join()
print(account.balance)


account2 = Account('小明', '23782738738', 1000)


list1 = [1, 2, 3]
lock = Lock()

def func1():
    lock.acquire()
    global list1
    list2 = list1[:]
    sleep(3)
    list2.append(100)
    list1 = list2[:]
    lock.release()


def func2():
    lock.acquire()
    global list1
    list2 = list1[:]
    sleep(3)
    list2.remove(2)
    list1 = list2[:]
    lock.release()


t1 = Thread(target=func1)
t2 = Thread(target=func2)
t1.start()
t2.start()
t1.join()
t2.join()
print(list1)

你可能感兴趣的:(线程数据安全_笔记)