PythonDay_07
2019-08-26 19:40
141 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/PhilBk/article/details/100019157
- 主线程与子线程
import _thread # 多线程 import win32api def show(i): # 0 代表系统,你真帅代表内容,来自JOKER代表标题,0代表窗口类型0,1,2,3 mystr = win32api.MessageBox(0,"你真帅","来自Joker的问候",0) for i in range(5): # 这是小弟线程 _thread.start_new_thread(show,(i,)) # 前面是执行函数,后面是一个元组,可以不写前提是函数没有形参 while True: # 在这里加入死循环是为了脚本主线程不死,小弟线程才能运行 pass
- 多线程的速度
import _thread import time def go(): for i in range(5): print(i,"-------") time.sleep(1) for i in range(5): # 同时执行5次 _thread.start_new_thread(go,()) for j in range(6): # 让主线程卡顿6秒 time.sleep(0.5) print("over") 结果: 0 ------- 0 ------- 0 ------- 0 ------- 0 ------- 1 ------- 1 ------- 1 ------- 1 ------- 1 ------- 2 ------- 2 ------- 2 ------- 2 ------- 2 ------- 3 ------- 3 ------- 3 ------- 3 ------- 3 ------- 4 ------- 4 ------- 4 ------- 4 ------- 4 ------- over
- 线程冲突
import _thread num = 0 def add(): for _ in range(1000000): global num num += 1 print(num) ''' for j in range(5): add() ''' for i in range(5): _thread.start_new_thread(add,()) # 这里就是线程冲突,5个线程同时抢夺num的资源,导致最后结果错误 while True: # 防止主线程不死 pass 结果: 1144840 1488805 1671079 1700819 1920182
- 基于类实现多线程
import threading import win32api class Mythread(threading.Thread): # 继承threading.Thread类 def run(self): # 重写threading.Thread类中的run函数 win32api.MessageBox(0,"hello",'joker',0) for i in range(5): # 同时创建5个线程 t = Mythread() # 初始化 t.start() # 开启 while True: pass
结果:
- 类线程风格
import threading import win32api class Mythread(threading.Thread): # 继承threading.Thread类 def run(self): # 定义函数 win32api.MessageBox(0,"hello",'joker',0) for i in range(5): t = Mythread() # 初始化 t.start() # 开启 # 等待一个线程执行完毕,再执行下一个线程,一方面可以阻止脚本主线程死掉,另一方面也可以防止线程冲突的一种办法 t.join() # t.join() 如果将其放在外部的不确定因素是,系统给for 循环和下面的代码锁定了一片内存,当循环执行完成之后, # 内存就开锁了,但是里面的东西还依然存在,所以才结束一个窗体,game over就出来了, # 就和删除文件后,内存中可能还有文件一样的道理 print("game over") 结果: game over
- 类线程的乱序风格
import threading import win32api class Mythread(threading.Thread): # 继承threading.Thread类 def __init__(self, num): threading.Thread.__init__(self) # 父类初始化 self.num = num def run(self): # 定义函数 win32api.MessageBox(0, "hello" + str(self.num), 'joker', 0) print(self.getName()) # 获取线程名 #name 是当前线程的属性, getName 是当前线程的方法。尽管 threading.current_thread().name threading.current_thread().getName() 的结果一样,但是完全不是同一种东西呀, 例如通过threading.current_thread().name = 'thread_python' 来改变它。 Mythd = [] for i in range(5): t = Mythread(i) # 初始化 print(i) t.start() # 开启 Mythd.append(t) # 将乱序线程(同时抢夺run这个函数)加入列表 for j in Mythd: # 这里与顺序不同,上面显示所有的线程都加入Mthd列表(所以一次性跳出5个窗口,但是主线程还没死,因为有join卡住)。 # j是线程 j.join() # 这里主线程同时等待所有线程都执行完毕,才执行“game over” print("game over") 结果: 0 1 2 3 4 Thread-3 Thread-1 Thread-4 Thread-5 Thread-2 game over
- 基于类解决线程冲突
import threading num = 0 mutex = threading.Lock() # 创建一个锁,threading.Lock()是一个类 class Myhtread(threading.Thread): def run(self): global num if mutex.acquire(1): # 如果锁成功,那么线程继续干活,如果锁失败,下面的线程一直等待锁成功,1,代表独占 for i in range(1000): # 数字小的时候还是不会产生线程冲突的 num += 1 mutex.release() # 释放锁,一定切记 print(num) mythread = [] for i in range(5): t = Myhtread() t.start() mythread.append(t) for thread in mythread: thread.join() # 或者直接将thread.join()加入for i in range(5),也能解决线程冲突,但是貌似就变成单线程了 print("game over") 结果: 1000 2000 3000 4000 5000 game over
相关文章推荐
- 重学Python - Day 07 - python基础 -> linux命令行学习 -- 常用命令 一
- python 自动化之路 day 07 面向对象基础
- python Day18 linux基础4
- python知识点 07-11
- Python100天入门到精通之Day2详解
- Python Day Nine
- 2_07_python基础学习_0328
- DAY14 PYTHON 数据类型 字符串
- Python Day2 小白知识 2(数据类型)
- Python学习——A Day at the Supermarket小项目
- Python Day13
- python day five
- Python进阶07 函数对象
- python get this week days and last week day maybe help for you
- 20171123Python07_循环和函数
- python Day 4
- python习题实例(上)_update18/07/03
- python-day-20
- 学习笔记(07):21天通关Python(视频课)-序列相关函数与封包解包
- day_07