Python中多线程thread与threading的实现方法
Python中多线程thread与threading的实现方法
学过Python的人应该都知道,Python是支持多线程的,并且是native的线程。本文主要是通过thread和threading这两个模块来实现多线程的。
python的thread模块是比较底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便的被使用。
这里需要提一下的是python对线程的支持还不够完善,不能利用多CPU,但是下个版本的python中已经考虑改进这点,让我们拭目以待吧。
threading模块里面主要是对一些线程的操作对象化了,创建了叫Thread的class。
一般来说,使用线程有两种模式,一种是创建线程要执行的函数,把这个函数传递进Thread对象里,让它来执行;另一种是直接从Thread继承,创建一个新的class,把线程执行的代码放到这个新的 class里。
我们来看看这两种做法吧。
一、Python thread实现多线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#-*- encoding: gb2312 -*-
import string, threading, time
def thread_main(a):
global count, mutex
# 获得线程名
threadname = threading.currentThread().getName()
for x in xrange ( 0 , int (a)):
# 取得锁
mutex.acquire()
count = count + 1
# 释放锁
mutex.release()
print threadname, x, count
time.sleep( 1 )
def main(num):
global count, mutex
threads = []
count = 1
# 创建一个锁
mutex = threading.Lock()
# 先创建线程对象
for x in xrange ( 0 , num):
threads.append(threading.Thread(target = thread_main, args = ( 10 ,)))
# 启动所有线程
for t in threads:
t.start()
# 主线程中等待所有子线程退出
for t in threads:
t.join()
if __name__ = = '__main__' :
num = 4
# 创建4个线程
main( 4 )
|
二、Python threading实现多线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#-*- encoding: gb2312 -*-
import threading
import time
class Test(threading.Thread):
def __init__( self , num):
threading.Thread.__init__( self )
self ._run_num = num
def run( self ):
global count, mutex
threadname = threading.currentThread().getName()
for x in xrange ( 0 , int ( self ._run_num)):
mutex.acquire()
count = count + 1
mutex.release()
print threadname, x, count
time.sleep( 1 )
if __name__ = = '__main__' :
global count, mutex
threads = []
num = 4
count = 1
# 创建锁
mutex = threading.Lock()
# 创建线程对象
for x in xrange ( 0 , num):
threads.append(Test( 10 ))
# 启动线程
for t in threads:
t.start()
# 等待子线程结束
for t in threads:
t.join()
|
相信本文所述Python多线程实例对大家的Python程序设计能够起到一定的借鉴价值。
Python中多线程thread与threading的实现方法相关推荐
- python的多线程threading_Python中多线程thread与threading的实现方法,pythonthreading
Python中多线程thread与threading的实现方法,pythonthreading 学过Python的人应该都知道,Python是支持多线程的,并且是native的线程.本文主要是通过th ...
- Python 中多线程共享全局变量的问题
Python 中多个线程之间是可以共享全局变量的数据的. 但是,多线程共享全局变量是会出问题的. 假设两个线程 t1 和 t2 都要对全局变量 g_num (默认是0)进行加1运算,t1 和 t2 都 ...
- python创建多线程_初学者看过来:Python中多线程和多处理的指南
使用Python分析数据,如果使用了正确的数据结构和算法,有时可以大量提高程序的速度.实现此目的的一种方法是使用Muiltithreading(多线程)或Multiprocessing(多重处理). ...
- python csv库,Python 中导入csv数据的三种方法
Python 中导入csv数据的三种方法,具体内容如下所示: 1.通过标准的Python库导入CSV文件: Python提供了一个标准的类库CSV文件.这个类库中的reader()函数用来导入CSV文 ...
- python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?
原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...
- python使用教程cmd啥意思-对python中执行DOS命令的3种方法总结
1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在Linux上执行的信息. import os os.system("ls") 2. ...
- python条件语句-Python中条件判断语句的简单使用方法
最简单的条件语句: if expression: expr_true_suite 如上,if是关键字,expression是条件表达式,条件表达式支持多重条件判断,可以用布尔操作符and.or和not ...
- python调用shell命令-python中执行shell命令的几个方法小结
最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码 代码如下: os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0或 ...
- 小结两种在Python中导入C语言扩展库的方法
小结两种在Python中导入C语言扩展库的方法 分类: Pythoner2009-08-18 20:44 2563人阅读 评论(1) 收藏 举报 python扩展c语言importstring 一种是 ...
最新文章
- Cordova+jQuery Mobile+Spring REST
- Fedora12下安装NCTUns6.0
- 转 推荐 33 个 IDEA 最牛配置,写代码太爽了!
- android源码出现的@字符意义总结
- 记录 FreeBSD
- python android自动化基于java_Appium+Python自动化 1 环境搭建(适用windows系统-Android移动端自动化)...
- P3389 【模板】高斯消元法
- python童年_300行Python代码实现俄罗斯方块,致敬逝去的童年
- 基于STM32和W5500的Modbus TCP通讯
- 计算机教学与实际生活相结合,小学数学教学与实际生活相结合的方法思考
- wow mysql dbc_WoW的DBC文件内容描述-魔兽有爱论坛 - 一个魔兽世界爱好者的聚集地 uiwow.com - Powered by Discuz!...
- 自动驾驶 9-4: 改进的 EKF - 错误状态扩展卡尔曼滤波器 An Improved EKF - The Error State Extended Kalman Filter
- 反地理编码 高德地图_由中文地址返回点位坐标-地理编码脚本分享
- BoundsChecker使用
- VB获取一个文件夹中指定的文件或文件夹名称到列表
- PHP电子合同对接流程,电子合同订立程序步骤
- windows 7 下,如何统计某文件夹下 视频总时长
- html实现给微信发红包看照片,微信发红包看图片效果实现
- 莫比乌斯(Mobius)反演知识整合
- C 生化危机 SDUT
热门文章
- ORACLE中用户解锁与改密(以hr用户为例)
- 研究Ajax(二)WebService实现加 减 乘 除
- python写二进制大文件,如何将文件写入二进制文件,或在大文件中编辑单行– Python...
- Android中关于Volley的使用(一)加载图片
- android中json解析及使用 (下)
- Android编译环境——VMware虚拟机安装配置
- 开发者论坛一周精粹(第十四期):CVE-2017-7529:Nginx敏感信息泄露
- Android可视化环境配置搭建方法
- shell脚本 linux
- T-SQL笔记7:临时表和表变量