tkinter 多线程+异步实现


import tkinter as tk
import asyncio
import random
import threading

class MainWindow(tk.Tk):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.geometry('500x500')
        self.a=tk.StringVar(value='0')
        self.b=tk.StringVar(value='1')

        self.label=tk.Label(self,width=20,height=2,bg='gray',textvariable=self.a)
        self.label.place(x=100,y=30)

        self.label2=tk.Label(self,width=20,height=2,bg='green',textvariable=self.b)
        self.label2.place(x=100,y=100)

        self.btn=tk.Button(self,text='start',command=self.start)
        self.btn.place(x=100,y=300)

    async def f1(self):
        while True:
            a=random.randint(100,1000)
            self.a.set(str(a))
            await asyncio.sleep(1)
    async def f2(self):
        while True:
            b=random.randint(10,99)
            self.b.set(str(b))
            await asyncio.sleep(3)

    async def main(self):
        tasks=[
            asyncio.create_task(self.f1()),
            asyncio.create_task(self.f2())
        ]
        await asyncio.wait(tasks)

    def run_thread(self):
        asyncio.run(self.main())

    def start(self):
        t=threading.Thread(target=self.run_thread)
        t.start()



if __name__ == '__main__':
    MainWindow().mainloop()
#上面的例子没有加入停止线程和异步事件循环。下面的例子加入了该功能
import threading
import tkinter as tk
import asyncio
import random

class Scal:
    def __init__(self,id):
        self.id=id
        self.flag=False
    async def start_scal(self):
        while not self.flag:
            await asyncio.sleep(3)
            print(self.id,'---',random.randint(10,100))
            print('--'*30)
    def stop(self):
        self.flag=True #用来退出循环
        loop=asyncio.get_event_loop()
        loop.stop()
        print('异步停止了')

async def run_asyan():
    tasks=[
        asyncio.create_task(obj.start_scal()),
        asyncio.create_task(obj2.start_scal()),
        asyncio.create_task(obj3.start_scal()),
        asyncio.create_task(obj4.start_scal()),
    ]
    await asyncio.wait(tasks)

def run_task():
    asyncio.run(run_asyan())

def on_colsing():
    obj.stop()
    obj2.stop()
    obj3.stop()
    obj4.stop()
    root.destroy()


def fun():
    t.start()


root=tk.Tk()

obj=Scal(1)
obj2=Scal(2)
obj3=Scal(3)
obj4=Scal(4)





root.geometry("500x400")
tk.Entry(root).pack()
tk.Button(root,text='click',command=fun).pack(pady=20)
t=threading.Thread(target=run_task)
root.protocol("WM_DELETE_WINDOW",on_colsing)


root.mainloop()
'''
#守护线程是一种特殊类型的线程,它的生命周期与主线程相关联。当所有的非守护线程结束时,守护线程会随##之自动结束。

#具体来说,守护线程的存在并不会阻止程序的退出。当所有的非守护线程结束时,Python 解释器会自动退#出,而不管守护线程是否还在执行。

守护线程通常用于执行一些后台任务或服务,它们不会阻塞程序的正常退出。例如,你可以创建一个守护线程用于定时更新某个数据,而不需要显式地停止守护线程。

需要注意的是,守护线程的特性决定了它们可能无法完整地执行完任务。因为它们会在程序退出时被强制终止,所以不能依赖守护线程来执行一些关键任务,比如文件的保存或数据库的操作。这些任务应该交给非守护线程来处理,以确保它们能够完整地执行。

在 Python 的 threading 模块中,可以通过设置线程对象的 daemon 属性为 True 来将线程设置为守护线程。例如,`threading.Thread(daemon=True)`。

总结起来,守护线程的主要特点是:
- 它的生命周期与主线程相关联。
- 当所有的非守护线程结束时,守护线程会随之自动结束。
- 它通常用于执行一些后台任务或服务,不会阻塞程序的正常退出。
- 它可能无法完整地执行完任务,不应该用于关键任务。
'''
import threading
import tkinter as tk
import asyncio
import random

class Scal:
    def __init__(self,id):
        self.id=id

    async def start_scal(self):
        while True:
            await asyncio.sleep(random.randint(1,3))
            print(self.id,'---',random.randint(10,100))
            print('--'*30)
    def stop(self):
        loop=asyncio.get_event_loop()
        loop.stop()
        print('异步停止了')

async def run_asyan():
    tasks=[
        asyncio.create_task(obj.start_scal()),
        asyncio.create_task(obj2.start_scal()),
        asyncio.create_task(obj3.start_scal()),
        asyncio.create_task(obj4.start_scal()),
    ]
    await asyncio.wait(tasks)

def run_task():
    asyncio.run(run_asyan())

def on_colsing():
    obj.stop()
    obj2.stop()
    obj3.stop()
    obj4.stop()
    root.destroy()


def fun():
    t.start()


root=tk.Tk()

obj=Scal(1)
obj2=Scal(2)
obj3=Scal(3)
obj4=Scal(4)





root.geometry("500x400")
tk.Entry(root).pack()
tk.Button(root,text='click',command=fun).pack(pady=20)
t=threading.Thread(target=run_task,daemon=True)
root.protocol("WM_DELETE_WINDOW",on_colsing)


root.mainloop()


#子线程用法
import threading
import asyncio
import random
import time
import tkinter as tk

class MyThread(threading.Thread):
    def __init__(self):
        super(MyThread, self).__init__()
        self.flag=False

    def run(self):
        while not self.flag:
            print('子线程执行中......')
            time.sleep(14)

    def stop(self):
        self.flag=True

t=MyThread()
t.start()

#主线程等一段时间后,停止子线程
time.sleep(2)
t.stop()

#等待子线程结束
t.join()
print("主线程结束")


#使用线程池和协程实现高并发

import asyncio
import time
from concurrent.futures import ThreadPoolExecutor
def download_pic(url):
    print(f"下载图片{url}")
    time.sleep(1)
    print(f"结束下载图片{url}")

async def main():
    ex= ThreadPoolExecutor(1000)
    loop=asyncio.get_running_loop()
    tasks=[]
    for i in range(10000):
        t=loop.run_in_executor(ex,download_pic,i)
        tasks.append(t)
    await asyncio.wait(tasks)

asyncio.run(main())

你可能感兴趣的:(python)