python3 多线程模拟格式化文本编辑器

# -*- coding: utf-8 -*-
from threading import Thread

talk_l = []
format_l = []
def talk():
    '''用户输入'''
    while 1:
        inp = input(">>: ").strip()
        if not inp:continue
        talk_l.append(inp)


def format():
    '''格式化'''
    while 1:
        if talk_l:
            res = talk_l.pop()
            res = res.upper()
            format_l.append(res)


def save():
    '''保存'''
    while 1:
        if format_l:
            with open("db.txt", "a", encoding="utf-8") as f:
                res = format_l.pop()
                f.write("%s\n" %res)


if __name__ == '__main__':
    '''开启3个线程'''
    t1 = Thread(target=talk)
    t2 = Thread(target=format)
    t3 = Thread(target=save)
    t_l = [t1, t2, t3]
    for t in t_l:
        t.start()

猜你喜欢

转载自www.cnblogs.com/lilyxiaoyy/p/11025781.html