Python多线程编程简单介绍


Posted in Python onApril 13, 2015

创建线程

格式如下

threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

这个构造器必须用关键字传参调用
- group 线程组
- target 执行方法
- name 线程名字
- args target执行的元组参数
- kwargs target执行的字典参数

Thread对象函数

函数 描述
start() 开始线程的执行
run() 定义线程的功能的函数(一般会被子类重写)
join(timeout=None) 程序挂起,直到线程结束;如果给了 timeout,则最多阻塞 timeout 秒
getName() 返回线程的名字
setName(name) 设置线程的名字
isAlive() 布尔标志,表示这个线程是否还在运行中
isDaemon() 返回线程的 daemon 标志
setDaemon(daemonic) 把线程的 daemon 标志设为 daemonic(一定要在调用 start()函数前调用)

常用示例

格式

import threading
def run(*arg, **karg):

    pass

thread = threading.Thread(target = run, name = "default", args = (), kwargs = {})

thread.start()

实例
#!/usr/bin/python

#coding=utf-8
import threading

from time import ctime,sleep
def sing(*arg):

    print "sing start: ", arg

    sleep(1)

    print "sing stop"


def dance(*arg):

    print "dance start: ", arg

    sleep(1)

    print "dance stop"
threads = []
#创建线程对象

t1 = threading.Thread(target = sing, name = 'singThread', args = ('raise me up',))

threads.append(t1)
t2 = threading.Thread(target = dance, name = 'danceThread', args = ('Rup',))

threads.append(t2)
#开始线程

t1.start()

t2.start()
#等待线程结束

for t in threads:

    t.join()
print "game over"

输出
sing start:  ('raise me up',)

dance start:  ('Rup',)

sing stop

dance stop

game over
Python 相关文章推荐
基于Python的身份证号码自动生成程序
Aug 15 Python
跟老齐学Python之变量和参数
Oct 10 Python
python数据类型判断type与isinstance的区别实例解析
Oct 31 Python
利用Python-iGraph如何绘制贴吧/微博的好友关系图详解
Nov 02 Python
基于Django contrib Comments 评论模块(详解)
Dec 08 Python
Python实现读取及写入csv文件的方法示例
Jan 12 Python
pandas每次多Sheet写入文件的方法
Dec 10 Python
python 控制Asterisk AMI接口外呼电话的例子
Aug 08 Python
Python打印不合法的文件名
Jul 31 Python
python与c语言的语法有哪些不一样的
Sep 13 Python
利用python如何实现猫捉老鼠小游戏
Dec 04 Python
python3 通过 pybind11 使用Eigen加速代码的步骤详解
Dec 07 Python
Python中的面向对象编程详解(下)
Apr 13 #Python
简单介绍利用TK在Python下进行GUI编程的教程
Apr 13 #Python
Python中的面向对象编程详解(上)
Apr 13 #Python
进一步理解Python中的函数编程
Apr 13 #Python
Python中的异常处理简明介绍
Apr 13 #Python
python中的装饰器详解
Apr 13 #Python
Python生成器(Generator)详解
Apr 13 #Python
You might like
苏联队长,苏联超人蝙蝠侠,这些登场的“山寨”英雄真的很严肃
2020/04/09 欧美动漫
php使用fgetcsv读取csv文件出现乱码的解决方法
2014/11/08 PHP
浅析PHP7的多进程及实例源码
2019/04/14 PHP
取得窗口大小 兼容所有浏览器的js代码
2011/08/09 Javascript
jquery实现textarea输入字符控制(仿微博输入控制字符)
2013/04/26 Javascript
跨域传值即主页面与iframe之间互相传值
2013/12/09 Javascript
JS+CSS实现表格高亮的方法
2015/08/05 Javascript
js删除局部变量的实现方法
2016/06/25 Javascript
Angularjs 动态改变title标题(兼容ios)
2016/12/29 Javascript
layer.alert自定义关闭回调事件的方法
2019/09/27 Javascript
JavaScript Window窗口对象属性和使用方法
2020/01/19 Javascript
Electron+vue从零开始打造一个本地播放器的方法示例
2020/10/27 Javascript
[02:54]DOTA2亚洲邀请赛 VG战队出场宣传片
2015/02/07 DOTA
python解析html开发库pyquery使用方法
2014/02/07 Python
Python入门篇之字典
2014/10/17 Python
解读Python中degrees()方法的使用
2015/05/18 Python
python数据处理实战(必看篇)
2017/06/11 Python
基于Python的关键字监控及告警
2017/07/06 Python
python 多维切片之冒号和三个点的用法介绍
2018/04/19 Python
python 字典修改键(key)的几种方法
2018/08/10 Python
Flask Web开发入门之文件上传(八)
2018/08/17 Python
python 函数的缺省参数使用注意事项分析
2019/09/17 Python
使用Python的datetime库处理时间(RPA流程)
2019/11/24 Python
TFRecord格式存储数据与队列读取实例
2020/01/21 Python
python实现飞船游戏的纵向移动
2020/04/24 Python
如何安装并在pycharm使用selenium的方法
2020/04/30 Python
导致python中import错误的原因是什么
2020/07/01 Python
德国汽车零件和汽车配件网上商店:kfzteile24
2018/11/14 全球购物
党支部书记岗位责任制
2014/02/11 职场文书
大学迎新晚会主持词
2014/03/24 职场文书
中国梦演讲稿5分钟
2014/08/19 职场文书
文明单位汇报材料
2014/12/24 职场文书
建筑工程材料员岗位职责
2015/04/11 职场文书
Nginx配置https原理及实现过程详解
2021/03/31 Servers
MySQL 角色(role)功能介绍
2021/04/24 MySQL
前端vue+express实现文件的上传下载示例
2022/02/18 Vue.js