threading 常见用法:

(1) 在 thread 中,我们是通过 thread.start_new_thread(function, args) 来开启一个线程,接收一个函数和该函数的参数,函数的参数必须是元组的形式
(2) 在 threading 中,我们是通过 threading.Thread(target=function, args=(....)) 来创建一个对象,然后再通过对象的 start() 方法来开启一个线程,多个线程可以并发执行

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun():
print time.ctime()
time.sleep(1) for i in range(3):
t = threading.Thread(target=fun, args=()) # 创建一个 Thread 对象,接收一个函数和该函数的参数
t.start() # 通过对象来开启一个新的线程,通过线程来执行函数
[root@localhost ~]$ python 1.py    # 多个线程并发执行,只需要执行一秒
Mon Jan 28 21:15:35 2019
Mon Jan 28 21:15:35 2019
Mon Jan 28 21:15:35 2019

threading 有两种方法可以开启一个新的线程:

(1) 如上面的例子,通过创建 Thread 对象,然后通过对象的 start() 方法来运行一个新的线程
(2) 第二种方法是继承 threading.Thread 这个类, 然后重写 run() 方法来运行一个新的线程

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading class MyThread(threading.Thread): def __init__(self):
threading.Thread.__init__(self) def run(self):
print time.ctime()
time.sleep(1) for i in range(3):
t = MyThread()
t.start()
[root@localhost ~]$ python 1.py
Mon Jan 28 23:53:02 2019
Mon Jan 28 23:53:02 2019
Mon Jan 28 23:53:02 2019

threading.Lock():用于创建一个锁对象,我们可以同时开启多个线程,但是在任意时刻只能有一个线程能在解释器运行,因此需要由全局解锁器(GIL)控制运行哪个线程

锁对象的常用方法如下:

lock = threading.Lock() 创建一个锁对象
    lock.acquire() 锁上锁,当某个线程被锁上之后,会优先执行这个锁的内容,直到锁释放才会执行其他线程
    lock.locked() 查看锁的状态,如果已经被锁上了则返回True,否则返回False
    lock.release() 释放锁,必须先获得锁才能释放锁

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def hello():
for i in range(3):
hello_lock.acquire()
print('hello'),
time.sleep(1)
world_lock.release() def world():
for i in range(3):
world_lock.acquire()
print('world')
time.sleep(1)
hello_lock.release() hello_lock = threading.Lock()
world_lock = threading.Lock() world_lock.acquire()
t1 = threading.Thread(target=hello, args=())
t2 = threading.Thread(target=world, args=()) t1.start()
t2.start()
[root@localhost ~]$ python 1.py
hello world
hello world
hello world

其他常用方法:

t.join()方法用于阻塞下一个线程的运行,等待当前线程执行完再执行下一个线程,例子如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun():
print time.ctime()
time.sleep(1) for i in range(3):
t = threading.Thread(target=fun, args=())
t.start()
t.join()
[root@localhost ~]$ python 1.py     # 由于线程被阻塞,需要执行3秒
Mon Jan 28 21:33:35 2019
Mon Jan 28 21:33:36 2019
Mon Jan 28 21:33:37 2019

t.setDaemon()方法用于设置当前线程随着主线程的退出而退出,例子如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun(): # 函数的功能是打印三次当前时间
for i in range(3):
print time.ctime()
time.sleep(1) t = threading.Thread(target=fun, args=())
t.setDaemon(True)
t.start() print 'End...' # 执行 print 之后,主线程就退出了,最终结果只会打印一次
[root@localhost ~]$ python 1.py
Mon Jan 28 21:54:48 2019
End...

t.getName() 用于获取线程的名字,t.setName() 用于重新设置线程的名字,默认的线程名是 Thread-1,Thread-2,Thread-3,......

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun():
newName = 'new-' + t.getName()
print newName
time.sleep(1) for i in range(3):
t = threading.Thread(target=fun, args=())
t.start()
[root@localhost ~]$ python 1.py
new-Thread-1
new-Thread-2
new-Thread-3

最新文章

  1. FTP概述
  2. Apache多站点配置及启动失败解决办法
  3. 解决firefox和IE9对icon font字体的跨域访问问题
  4. 将struts源码导入eclipse
  5. Windows下绘制数学函数图像的方法
  6. oracle注意事项
  7. MyEclipse2014安装ADT插件(适用于其他版本)
  8. shell脚本加密
  9. Asp.net MVC5中Html.DropDownList的使用
  10. Ubuntu下OpenVPN客户端配置教程
  11. Spring Security(14)——权限鉴定基础
  12. Git 常用命令速查表(三)
  13. SIT 和 UAT
  14. Codeforces Round 1152 (div. 2)
  15. 数据结构与算法JS实现
  16. selenium中的上传文件
  17. 类 __getitem__ __getattr__ __call__
  18. Python学习第十四篇——类初步使用及面向对象思想
  19. 【黑客免杀攻防】读书笔记8 - 软件逆向工程基础2(if-else,三目运算符)
  20. CODEVS.3990.中国余数定理2(CRT)

热门文章

  1. python 实现ARP攻击
  2. python基础系列教程——Python库的安装与卸载
  3. web编码
  4. 【C#】关闭 Window 之后,无法设置 Visibility,也无法调用 Show、ShowDialogor 或 WindowInteropHelper.EnsureHandle
  5. 解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题
  6. js学习笔记34----自执行函数
  7. 基于Java的四大开源测试工具
  8. [android] Android 错误集锦
  9. Windoows窗口程序七
  10. 安装cx_Oracle 遇到的杂项问题