python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
来源:互联网 发布:unity3d人才过剩 编辑:程序博客网 时间:2024/05/22 15:49
进程是资源分布的单元
线程是进程中真正执行代码的
进程运行起来,会有一个主线程进行运行
父子线程:相互独立运行,当所有的子线程执行完后,主线程才执行完
下面这个程序就是一个线程
#线程也是python实现多任务的一种方式,thread模块是比较底层的模块#threading模块是对thread做了一些包装,更方便使用#多线程的执行import threadingimport timedef sayHello():#多个线程去执行一个函数,完全可以print("hello")time.sleep(2)if __name__=="__main__":for i in range(5):t=threading.Thread(target=sayHello)#同时执行,而不是一个一个执行t.start()#创建一个新的线程,去target中执行,这里开启 了5个线程'''hellohellohellohellohello'''
创建线程的另外一种方式:
import threadingimport timeclass MyThread(threading.Thread):def run(self):for i in range(3):time.sleep(1)msg="I'm" +self.name+'@'+str(i) #name属性中保存的是当前线程的名字print(msg)if __name__=='__main__':t=MyThread()#实例化类的对象就可以调用t.start()'''I'mThread-1@0I'mThread-1@1I'mThread-1@2'''
父子进程和父子线程的执行顺序不一定,由操作系统的调度算法决定
import threadingfrom threading import Threadimport timethnum=100class MyThread(threading.Thread):def run(self):for i in range(3):global thnumthnum+=100time.sleep(1)msg="I'm" +self.name+'@'+str(i) #name属性中保存的是当前线程的名字print(msg)print(thnum)def test():global thnumprint(thnum)if __name__=='__main__':t=MyThread()t.start()time.sleep(4)#保证第一个线程执行完thn=Thread(target=test)thn.start()'''I'mThread-1@0200I'mThread-1@1300I'mThread-1@2400400'''
线程使用全局变量的弊端,一旦设置不好时间,则输出错误的结果
import threadingfrom threading import Threadimport timethnum=0class MyThread(threading.Thread):def run(self):for i in range(10000):global thnumthnum+=1print(thnum)def test():global thnumfor i in range(10000):thnum+=1print(thnum)if __name__=='__main__':t=MyThread()t.start()time.sleep(4)#保证第一个线程执行完,这样的话运行结果为20000#但是如果将这句话屏蔽掉的话,就会发现结果不是20000,全局变量的值在其中有交叉,而不是先运行完第一个程序再运行第二个程序thn=Thread(target=test)thn.start()'''I'mThread-1@0200I'mThread-1@1300I'mThread-1@2400400'''
列表当做参数传递给线程,会当做全局变量
import threadingfrom threading import Threadimport timethnum=[11,22,33,44]class MyThread(threading.Thread):def run(self):for i in range(3):thnum.append(i)print(thnum)def test():print(thnum)if __name__=='__main__':t=MyThread()t.start()time.sleep(1)#保证第一个线程执行完thn=Thread(target=test)thn.start()'''[11, 22, 33, 44, 0, 1, 2][11, 22, 33, 44, 0, 1, 2]'''
避免全局变量被修改:
import threadingfrom threading import Threadimport timethnum=0f_flag=0class MyThread(threading.Thread):def run(self):for i in range(10000):global thnumthnum+=1global f_flagf_flag=1print(thnum)def test():global thnum#下面的f_flag是全局变量,但是这里没有进行修改,所有不用加globalwhile True: if f_flag!=0: for i in range(10000): thnum+=1 breakprint(thnum)if __name__=='__main__':t=MyThread()t.start()time.sleep(4)#保证第一个线程执行完,这样的话运行结果为20000#但是如果将这句话屏蔽掉的话,就会发现结果不是20000,全局变量的值在其中有交叉,而不是先运行完第一个程序再运行第二个程序thn=Thread(target=test)thn.start()'''1000020000'''
阅读全文
0 0
- python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
- linux线程使用读写锁共享全局变量
- 全局变量的线程安全性
- Python 全局变量的使用
- python全局变量的使用
- python unittest TestCase间共享数据(全局变量的使用)
- 多线程学习之一:线程对共享全局变量的访问
- python 中全局变量的修改
- 避免使用全局变量
- delphi 线程全局变量copy
- PKG全局变量线程安全问题
- 避免全局变量!
- 避免全局变量
- python线程的使用方法以及全局变量的应用
- python进阶之多线程对同一个全局变量的处理
- python中全局变量的使用
- python全局变量的使用错误
- VC++基础 使用全局变量控制线程运行
- wpf弹出窗,被主界面遮挡问题解决办法
- java 字节流与字符流的区别
- Redis和Memcached的区别
- shell paste指令:用于不同文件的合并粘贴
- Google VR Android开发(二)-Simplevideowidget分析
- python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
- 比较forward动作指令和include动作指令
- spring boot的相关配置
- mongodb 开启和关闭的方法总结
- Tensorflow安装
- jboss7.1.1类加载分析
- VMware虚拟机中CentOS/redhat设置固定IP
- 3、ES6 === string 模板字符串
- 两个JS之间的函数互相调用